tags:

views:

801

answers:

5

Hello,

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.

I hope someone understand what I am trying to say.

Many thanks for any advice,

+1  A: 

I'm using EDE from Cedet to configure projects for work with CMake. Look into my CEDET config, starting with line 105, for example of code for project configuration. I use separate Debug & Release directories for different builds, and by default, compilation is running only for Debug mode....

Alex Ott
I am quite new to emacs, and I have never written in lisp. I think you are setting the path to your CMakelists.txt and your boost libraries. However, I am not sure about this. cons 'compile-command 'alexott/gen-cmake-debug-compile-string" For my .emacs I have the following (global-set-key [f5] 'compile). Any suggestions would be most helpfull. Thanks.
robUK
my configuration uses F9 - see line 165it works following way. 'cons compile-command ...' is used to setup process-specific compilation command, that will applicable only for concrete project. This command will take project root directory, determined from ':file path_to_cmakelists.txt' and will perform 'cd PROJECT_ROOT/Debug; make -j2'
Alex Ott
+1  A: 

You can change the current working directory that emacs uses with M-x cd<enter> <path to build dir><enter>. After this, when you run M-x compile, make will be executed in the new working directory. This is effectively the same as running make on the terminal in the build directory, so it should work for you just fine.

liwp
Thanks, that worked ok.
robUK
+2  A: 

A more general solution for compile-command would be:

cd ${PWD%/src/*}/build && cmake ../src && make

The environment variable PWD holds the directory the file you're editing is in; expanding it with %/src/* gets you back to the project root without having to worry about how many ../s you need.

Porculus
+5  A: 

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.

Dmitry
Hello, this looks good. Where can I download the package dir-locals.el. Any documentation on how to use it? Thanks.
robUK
@robUK, I updated the answer.
Dmitry
Thanks. I will check that. Currently I am using 23.1.1.
robUK
@Dmitry: just to clarify, do you need to have a `.dir-locals.el` file in _each_ subdirectory for this to work? I'm working on a large scale project with a huge `src` tree and I can't add emacs-specific files to the repo so I would have to generate the `.dir-local.el` files each time I check out...
liwp
@liwp: No, .dir-locals.el files apply to subdirectories.
Charles Stewart
@liwp, I updated the answer.
Dmitry
I have marked this as the answer. I will I could have marked everyone correct.
robUK
+1  A: 

Change the command to use the absolute instead of relative pat. EG:

cmake ~/workspace/project/src && make
David Oneill