I have a directory tree like this:
libs
support
db
csv
patterns
support_qt
helpers
dialogs
etc.
Now when I do the add_subdirectory in the support level, I can add db and patterns and the files are collected. However in db I added another add_subdirectory referencing the csv, but somehow this is ignored.
In support
set(SUPPORT_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/support_defs.h
${CMAKE_CURRENT_SOURCE_DIR}/support_dll_api.h
${CMAKE_CURRENT_SOURCE_DIR}/supportlib_namespace.h
${CMAKE_CURRENT_SOURCE_DIR}/dll_main.cpp
)
add_subdirectory (db)
add_subdirectory (patterns)
In db
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp
PARENT_SCOPE
)
add_subdirectory(csv)
The above works fine but in csv
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/csv.h
${CMAKE_CURRENT_SOURCE_DIR}/csv.cpp
PARENT_SCOPE
)
But these files are not included in the build. So do I have to put the add_subdirectory calls all into the root file?
Just found the solution. I have to put the add_subdirectory before the set command.
add_subdirectory(csv)
set(SUPPORT_SOURCE ${SUPPORT_SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/column_types.h
${CMAKE_CURRENT_SOURCE_DIR}/dbcolumn.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.h
${CMAKE_CURRENT_SOURCE_DIR}/database_login.cpp
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/type_helper.cpp
PARENT_SCOPE
)
Related
I have directory aaa and aaa/bb. I want CMake to create the moc_compilation.o file only for aaa/*.cxx files, but it scans recursively aaa/bb/*.cxx files also. How can I disable the recursive scan for automoc?
You can use the SKIP_AUTOMOC source file property to skip automoc processing for one file, or an entire group of files. This works for header files as well:
file(GLOB MY_EXCLUDED_SOURCES aaa/bb/*.cxx)
set_property(SOURCE ${MY_EXCLUDED_SOURCES} PROPERTY SKIP_AUTOMOC ON)
I have a third-party CMake package that does some non-trivial work in its own CMakeLists.txt but doesn't set the resulting variables with PARENT_SCOPE so a CMakeLists.txt file that has added the project directory doesn't get to look at the variables.
Tacking a few set commands with PARENT_SCOPE to the end of the package's CMakeLists.txt works fine but is there any trick that would allow a parent scope to extract variables from a child scope?
You can use the get_directory_property command for that purpose:
add_subdirectory(sources)
...
get_directory_property(variableValue DIRECTORY sources DEFINITION variableName)
I'm trying to call add_library for all files with certain endings.
The dir structure is:
src
| - CMakeLists.txt (1)
| - main.cpp
| - gui
| - CMakeLists.txt (2)
| - some source and header files
So currently all cc files are in the gui directory.
(1) CMakeLists.txt:
file( GLOB_RECURSE my_sources *.cc )
message(STATUS "my_sources = ${my_sources}")
add_subdirectory( gui )
add_library( my_src ${my_SOURCES} )
target_link_libraries( my_src
my_gui
)
qt5_use_modules( my_src Core Gui Widgets)
(2) CMakeLists.txt:
file( GLOB my_gui_sources *.cc)
add_library( my_gui ${my_gui_sources} )
qt5_use_modules( my_gui Core Gui Widgets)
But I keep getting this output:
You have called ADD_LIBRARY for library my_src without any source files. This typically indicates a problem with your CMakeLists.txt file
-- my_sources = /home/bla/bla/src/gui/BorderLayout.cc;...;/home/bla/bla/my/src/gui/MainWindow.cc
-- my_gui_sources = /home/bla/bla/my/src/gui/BorderLayout.cc;...;/home/bla/bla/my/src/gui/MainWindow.cc
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bla/bla/my/build
I know that I currently don't need the add_library in the first CMakeLists.txt, but later I will. I changed the first GLOB to GLOB_RECURSE, so that it finds at least anything.
For some reason your
file( GLOB my_gui_sources *.cc *.h)
Is not finding any file. To debug, you can print:
message(STATUS "my_gui_sources = ${my_gui_sources}")
Probably you want to use GLOB_RECURSE, which search in sub-directories:
file( GLOB_RECURSE my_gui_sources *.cc *.h)
Note that you don't need to add headers files to the source list.
Take care that you will have to rerun cmake every time you add a file to your project (cmake won't be called automatically, thing that instead happens if you touch one of the cmake files).
Link to documentation of command "file"
Edit:
The actual problem is that in your first CMakeLists.txt file you are using inconsistent naming for your variable (note that casing is important), therefore you have to change your add_library command to:
add_library( my_src ${my_sources} )
Note (off the records :-) ): the fact that casing is important for variable names might be confusing because, on the other hand, in cmake command names are case insensitive. It's also sometimes weird to notice that the character - (minus) might be used as part of the variable name: using _ (underscore) is most of the time preferable.
I am using CMake to build a simple C++ project, hereafter named P. The structure of P is quite simple:
P/src/
P/src/package1
P/src/packege2
P/src/...
P/src/main-app
I would like to collect the libraries in package1, package2, ... in a variable called P_LIBS.
In a first attempt, I tried to collect the libraries available in package1, package2, ... in the variable called P_LIBS initially set in the src/CMakeLists.txt file. However, the updates to P_LIBS made in the CMakeLists.txt of the subfolders are not propagated to the parent folder.
I would rather not write a list of libraries in the main CMakeLists.txt file. I would rather modify such variable while moving in the directory tree.
After a search on the internet I could not find any valid suggestion. If I look at the various Find files, I only see long listings of libraries in their main CMakeLists.txt file.
Is there a way to do what (I hope) I explained above?
Thanks to sakra's link I was able to 'propagate' names up to the parent folder. However, the names I add to the P_LIBS variable are later interpreted as 'library' names, not as reference to CMake targets. In other words, if
P_LIBS = {a, b}
the 'a' and 'b' are interpreted as the library names, i.e. CMake generates:
gcc [...] -l a -o exe
instead of
gcc [...] /path/to/a.o -o exe
(.o or other extensions)
You are propably constructing the targets list as a string, try to make them a list instead. For example:
# in package1/CMakeLists.txt
set(P_LIBS ${P_LIBS} a b PARENT_SCOPE)
I have a directory with files that either belong to a set that makes up a Qt project, and other files that do not. That is, files A.cxx, ADriver.cxx and A.ui all belong to a set that needs to be compiled with Qt options. I then have a file B.cxx that is non-qt. Then C.cxx, CDriver, and C.ui are another Qt set. There are tens of these, so I want to use globs rather than write each add_executable manually. I was thinking of doing something like
for(all ui files)
create an executable from the ui and its matching .cxx and *Driver.cxx"
end
Then all cxx files that "remain" (not used in the above loop) are non-Qt, and need to be compiled by themselves. My question is how to "subtract" files from a "set". That is, to use the method described above I'd have to have a set of all cxx files, and remove the ones that get used in the .ui file loop. Is this possible? Is there a better way to do something like this?
First, gather all files with a glob:
file(GLOB ALL_SRCS *)
Then select ui files and create Qt targets for them, substracting them from the ALL_SRCS list at the same time:
file(GLOB UIS *.ui)
foreach(ui ${UIS})
get_filename_component(f ${ui} NAME_WE)
# Do Qt stuff
qt4_wrap_ui( ${f}uis ${ui} )
qt4_wrap_cpp( ${f}srcs ${f}.cpp ${f}Driver.cpp )
add_executable( ${f} ${f}uis ${f}srcs )
list(REMOVE_ITEM ALL_SRCS ${ui} ${f}.cpp ${f}Driver.cpp)
endforeach()
After this you'll have all non-qt sources in ALL_SRCS.