gcc 4.4.2
cmake 2.6
I have just started using cmake. Coming from writing my own Makefiles.
However, I have this directory structure and using out-of-source build. To separate the source files from the build files.
project
src
my_c_files.c
CMakeLists.txt
build
Makefile
Before I would write my own Makefiles in the src directory and then press F5 and I was able to compile my program. And emacs would list any warning or errors. However, it seems that I have to open a terminal and go to the build directory to run
cmake ../src
and then
make
I would like to run make from within emacs the same as pressing F5. But as the Makefile as been generated in the build directory, and emacs is looking for it in the src directory.
Even when I set a soft link to the Makefile in the src directory to link to the Makefile in the build directory. All this did was give me a list of errors.
For exactly the same purpose I found
.dir-locals.elto be extremely helpful, here’s how one of mine looks like:Obviously I can have paths specified in different
.dir_locals.elaccording to their location and such, say some buildrun_testsfor unit test, some build the real target and so on.Then I put a dummy
makefilein the build directory, which looks like this:This way I can make a checkout and just run
M-x compilein whichever file I like and it will do the right thing. I use git and have that Makefile ignored from monitoring by git like so:git update-index --assume-unchanged Makefileand if I want to modify and commit it I do
git update-index --no-assume-unchanged Makefile.This way the newly created by cmake Makefile won’t show in
git statusas modified, and I won’t commit it accidentally.Benefits of this approach:
since cmake uses absolute paths internally, there’s absolutely no problem jumping through compilation errors in compilation buffer by just pressing enter on them.
you can specify whatever indentation rules you want there, and they can be different in different projects or even directories if you so please 🙂
you can specify any amount of targets you want in different projects, even directories, still use the same old
M-x compile(I have it bound toC-c b) and get Emacs to do the right thing.The only drawback there is having
.dir-locals.elin your subdirectories, which I hardly find bad.EDIT: robUK, to answer your comment:
Here is documentation for Per-Directory Local Variables as they call it in Emacs manual. It is not a package, it’s a part of Emacs, although I’m not sure if it was available in pre-23 versions, I’m using 23.1.1.
EDIT2: liwp, to answer your question:
As Charles already pointed out, and as documentation says:
You don’t need
.dir-locals.eleverywhere in your project tree. Note, however, it probably depends on the complexity of your code base structure.I normally would go for a fairly simple layout like this:
and I would have slightly different targets specified in different
.dir-locals.el, say in/testI would only buildtestrunnerand have it executed, I’d spend most of the development time building this target. Then in/srcI would first build the sametestrunner, have it executed,and if everything goes well there, it would build me my main project target. If you don’t need this, you could be fine with just one.dir-locals.elunder/project_root_dir. If your source structure and requirements are a lot more sophisticated, it could mean you’d need a bit more path- and target-related wizardry.