I am trying to build a big project with CMake but I struggle on how to write the CMakeList.txt file.
My project is separated in different folders each containing a set of .hpp and .cpp files more or less related together as follows:
root
- memory
-- Memory.cpp
-- Memory.hpp
-- MemoryManager.hpp
-- MemoryManager.cpp
-- CMakeLists.txt
- tools
-- Array.cpp
-- Array.hpp
-- CMakeLists.txt
- main.cpp
- CMakeLists.txt
I would like to build all the files together to an executable. I don't want to build libraries in each subfolder as I don't see any good reason to do it.
I would like also to avoid putting a single big list of all source files in the ADD_EXECUTABLE command of the CMakeLists.txt file located at the root of the project.
Do you have any idea of how to set this up correctly ?
Cheers,
M.
You can use the GLOB function, such as:
file (GLOB _my_sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
memory/*.cpp tools/*.cpp main.cpp)
add_executable (myprogbin ${_my_sources})
set_target_properties (myprogbin PROPERTIES OUTPUT_NAME myprog)
See http://cmake.org/cmake/help/cmake-2-8-docs.html#command:file for reference
Related
How can I add several source files to ALL CMake add_executable()s (aka. targets)?
I am just writing something like an automated CMake script, so I have no prior knowledge of any target names (or variables) defined in normal projects.
I know everything about the source files that I want to add.
My script will be include()ed to CMakeLists.txt in other normal projects.
Is there an elegant way to do this besides iterating through BUILDSYSTEM_TARGETS and target_sources() for each target?
I have seen solutions that add sources files to some specific target:
Can one add further source files to an executable once defined?
To elaborate on what I want to do, check this:
# my_script.cmake
some_magic_function_that_adds_sources_to_all_targets(
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
# CMakeLists.txt (of some other projects)
cmake_minimum_required(VERSION 3.7)
project(project_name)
include(abs_path_to_my_script.cmake) # can be anywhere in this file
add_executable(exe1
exe1.cpp)
add_executable(exe2
exe2.cpp)
add_executable(i_dont_know_exec_name
i_dont_know.cpp
how_many_files.cpp
are_here.cpp)
As a result, the CMakeLists.txt will be functionally equivalent to:
# CMakeLists.txt (of some other projects)
cmake_minimum_required(VERSION 3.7)
project(project_name)
add_executable(exe1
exe1.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
add_executable(exe2
exe2.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
add_executable(i_dont_know_exec_name
i_dont_know.cpp
how_many_files.cpp
are_here.cpp
abs_path_to_file1.cpp
abs_path_to_file2.cpp
)
I have multiple projects that have identical/duplicated code in their respective CMakeLists.txt. At the very least I need a place to store string definitions that I can pull into the CMakeLists.txt files. What's the best way to do this? My directory structure
common_defs/
src_files
hdr_files
CMakeLists.Txt - file that has common defs
independent_dir1/
src_files
hdr_files
CMakeLists.Txt --> Imports/Includes from ../common_defs/CMakeLists.txt
independent_dir2/
src_files
hdr_files
CMakeLists.Txt --> Imports/Includes from ../common_defs/CMakeLists.txt
independent_dir1 and independent_dir2 will be built independently from each other. Building in common_defs should not trigger builds in the independent_dirs
Create a file in common_defs/common_defs.cmake with common strings and definitions, than add to your CMakeLists.txt:
// add path common_defs to include search path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../common_defs")
// include module from file common.cmake searched in search path
include(common_defs)
I Have configured a list of folders which will be considered for a build in a CMAKE file.But inside those folders there are few source file which will not taken for compilation while build.
How can I generate the list of files( C and C++ Source files) that are only considered for compilation for build using CMAKE.?
If you have multiple subfolders, you can add them by add_subdirectory and modify the CMakeLists.txt in the subfolders by accessing the ${SOURCE_FILES} variable in the parent scope by
SET(SOURCE_FILES ${SOURCE_FILES}
subFolder/yetAnotherFile.cpp
PARENT_SCOPE
)
In the main folder (the folder that includes the subfolder), you add the following to the CMakeLists.txt
SET(SOURCE_FILES main.cpp)
add_subdirectory(subFolder/
add_executable(program ${SOURCE_FILES})
My source tree:
/project
/project/src/<my sources>
/project/build/vs2008
/project/build/ubuntu
I want to put my CMakeLists.txt in vs2008 and ubuntu. I can accept put one CMakeLists.txt to each folder and put another global CMakeLists.txt on /project/build, but I just don't want any CMakeLists.txt in /project/src(So I can't use add_subdirectory command). I need my solution files of visual studio in /project/build/vs2008 and Makefile in /project/build/ubuntu. What commands I should know about?
I think there's nothing special you would need to know -- you just need to specify paths to your source files relative to where your CMakeLists.txt is located, and your targets should build the same as if you had placed CMakeLists.txt in your src folder. For example, project/build/ubuntu/CMakeLists.txt could look like this:
set( SrcDir ../../src )
add_executable( MyApp
${SrcDir}/file1.cpp
${SrcDir}/file2.cpp
# and so on
)
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})
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.