0

I'm writing a cmake file for a project which has the following structure

project/ (root)
  libraries/ (contains (precompiled) libraries
  src/
    code/ (contains a set of fortran files)

My CMakeLists.txt file is currently in project/ and effectively is just

cmake_minimum_required(VERSION 2.6)

enable_language(Fortran)
project(project1)

set(projsrc src/code)

set(libdir lib/)

find_library(PROJ_LIBRARY pr10 PATHS ${libdir})

add_executable (sc1 sc1.f90)
target_link_libraries(sc1 ${PROJ_LIBRARY})

This creates my binary in the same folder as the source code, when I actually want it in the level above (i.e. in the src folder - this structure will be changed so we have a bin folder eventually), but haven't worked out how to do it.

Some answers on SO say you have to have a CMakeLists.txt file in every folder - is this correct? Is it possible to set an environment variable or use a CMake variable (e.g. http://cmake.org/cmake/help/v2.8.8/cmake.html#command:set). It's also not very clear from some answers whether the solutions they have posted are C++ specific (as that is what language CMake most often seems to be used for).

Edit

I found out that I can change it to the behaviour I want by modifying it slightly:

cmake_minimum_required(VERSION 2.6)
enable_language(Fortran)
project(project1)

set(projsrc src/code)

set(libdir lib/)

find_library(PROJ_LIBRARY pr10 PATHS ${libdir})

add_executable (src/sc1 ${projsrc}/sc1.f90)
target_link_libraries(src/sc1 ${PROJ_LIBRARY})

However, this doesn't explain why my behaviour is different to how it should be, according to arrowdodger below. I'm also still trying to work out how to display the values of environment variables; I've tried the following with no luck:

message(${RUNTIME_OUTPUT_DIRECTORY})
message($ENV{RUNTIME_OUTPUT_DIRECTORY})
message(${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
message($ENV{CMAKE_RUNTIME_OUTPUT_DIRECTORY})
ChrisW
  • 4,970
  • 7
  • 55
  • 92
  • 1
    Be careful with your "bin" dir idea. This is not the way modern builds works, for good reasons. You may want "out-of-source builds" instead, see here : http://stackoverflow.com/a/12237398/587407 – Offirmo Oct 10 '12 at 15:07
  • @Offirmo - hmm, all the scientific software I use seems to have a `src` and `bin` dir structure - what's wrong with this idea? Any further reading you can point me to? – ChrisW Oct 10 '12 at 16:01
  • 2
    For example, if you want to build the same program with different compilers (I do that) or different options (I do that, too), how would you make them coexist in the bin dir ? You'll have to duplicate the full structure. With "out-of-source" builds, you can have as many "bin" dirs as you want, all sharing the same sources. cmake advocates it : http://www.cmake.org/Wiki/CMake_FAQ#Out-of-source_build_trees and, for example, the Wt lib builds like that : http://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html#build. – Offirmo Oct 10 '12 at 17:02

1 Answers1

0

By default, binaries will appear in respective subdirectory of your build dir. By respective i mean the directory that contains CMakeLists.txt with add_executable() call.

For example, if you have following CMakeLists.txt

add_executable(tgt1 src1.f90)
add_executable(tgt2 subdir/src2.f90)

in the root folder, you will get both binaries in ${CMAKE_BINARY_DIR}. So if you wish tgt2 to be in ${CMAKE_BINARY_DIR}/subdir, you need to add CMakeLists.txt there and call add_executable(tgt2 src2.f90) from there.

You can change this behavior:

CMAKE_LIBRARY_OUTPUT_DIRECTORY, CMAKE_RUNTIME_OUTPUT_DIRECTORY and others.

You can also set respective target properties.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Thanks for the hints; although my experience seems to be slightly different - the binaries are created 2 levels below that of my `CMakeLists.txt` file (`CMakeLists.txt` is in `root/`, binaries are created in `root/src/code`) - is there something I'm misunderstanding? In addition, how do you find out what `CMAKE_RUNTIME_OUTPUT_DIRECTORY` is currently set to? I couldn't find it in the docs – ChrisW Oct 09 '12 at 15:03
  • Hmm, strange. It might be something specific to Fortran. Can you post actual `CMakeLists.txt`? – arrowd Oct 09 '12 at 16:36
  • 1
    That's really strange behavior even for in-source build. Dunno. BTW, for displaying things you need `message(STATUS "blabla")` and for obtaining target properties - `get_target_property()` command. – arrowd Oct 10 '12 at 17:40