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.el
to be extremely helpful, here's how one of mine looks like:
((nil . ((tab-width . 8) (indent-tabs-mode . t))) (c++-mode . ((c-basic-offset . 8) (tab-width . 8) (indent-tabs-mode . t) (compile-command . "make -C ../build -j 2 run_tests"))) ((c-mode . ((c-basic-offset . 8) (tab-width . 8) (indent-tabs-mode . t) (compile-command . "make -C ../build -j 2 run_tests")))))
Obviously I can have paths specified in different .dir_locals.el
according to their location and such, say some build run_tests
for unit test, some build the real target and so on.
Then I put a dummy makefile
in the build directory, which looks like this:
all: run_tests run_tests: @cmake .. @make -j 2
This way I can make a checkout and just run M-x compile
in 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 Makefile
and 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 status
as 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 to C-c b
) and get Emacs to do the right thing.
The only drawback there is having .dir-locals.el
in 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:
If you put a file with a special name .dir-locals.el in a directory, Emacs will read it when it visits any file in that directory or any of its subdirectories, and apply the settings it specifies to the file's buffer. Emacs searches for .dir-locals.el starting in the directory of the visited file, and moving up the directory tree. (To avoid slowdown, this search is skipped for remote files.)
You don't need .dir-locals.el
everywhere 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:
/project_root_dir /build /src /test
and I would have slightly different targets specified in different .dir-locals.el
, say in /test
I would only build testrunner
and have it executed, I'd spend most of the development time building this target. Then in /src
I would first build the same testrunner
, 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.el
under /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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With