How to control subdirectory compiling order of cmake? - compilation

This is my CMakeLists.txt:
ADD_SUBDIRECTORY(third)
ADD_SUBDIRECTORY(utils)
ADD_SUBDIRECTORY(rpc)
But the directory 'rpc' will be compiled before directory 'utils', actually the 'rpc' is depends on 'utils', so I will get a link error.
How can I make the 'rpc' compiling after 'utils'?
Thanks.

When you use target_link_libraries() function and pass it other target name, CMake automatically sets this target as a dependency. You can also use add_dependencies() to specify dependencies manually.
Also note that order of sources compilation have nothing to do with your problem. Link errors (i guess, you are seeing "undefined reference" ones) are because you aren't linking your targets properly.

if the 'rpc' is depends on 'utils':
utils CMAKELISTS.txt
project(utils)
add_library (utils SHARED ${PROJECT_SOURCE_LIST})
rpc CMAKELISTS.txt
project(rpc)
add_library (rpc SHARED ${PROJECT_SOURCE_LIST})
# must add this command to scan dependencies of target rpc
add_dependencies (rpc utils)
target_link_libraries (${TEST_SOURCE_FILE_NAME} libutils.so)

I have the same question with you.
I have open source libs —— jthread and jrtplib. I want to build them by a main CmakeList.text so I make a folder like:
cpp
├─jrtplib
│ ├─src
│ └─CMakeFiles.txt
├─jthread
│ ├─src
│ └─CMakeFiles.txt
└─CMakeFiles.txt
and cpp/CMakeFiles.txt like:
cmake_minimum_required (VERSION 3.8)
set(JTHREAD_ENABLED 1)
# 包含子项目。
add_subdirectory ("jthread")
add_subdirectory ("jrtplib")
but build error, it build jrtplib first, I think "jr" > "jt", so build jrtplib first, and it can't find libjthread.so , so target_link_libraries error
I find a solution :
cmake_minimum_required (VERSION 3.8)
# ↓↓↓↓↓↓↓↓ attention ↓↓↓↓↓↓↓↓
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
# ↑↑↑↑↑↑↑↑ attention ↑↑↑↑↑↑↑↑
set(JTHREAD_ENABLED 1)
# copy jthread/src/*.h to C:/thirdParty/include/jthread
set(JTHREAD_INCLUDE_DIRS "C:/thirdParty/include")
set(JTHREAD_LIBRARIES "libjthread.so")
# 包含子项目。
add_subdirectory (jthread)
add_subdirectory (jrtplib)
set xx_OUTPUT_DIRECTORY can set library build in main cmake_build folder, so jrtplib can find libjthread.so .
build success ...

I am assuming that the project named "third" is independent however "utils" depends upon "rpc". Try the following code for a sequential build
ADD_SUBDIRECTORY(third)
ADD_SUBDIRECTORY(utils "${CMAKE_CURRENT_BINARY_DIR}/utils_build")
ADD_SUBDIRECTORY(rpc "${CMAKE_CURRENT_BINARY_DIR}/rpc_build")
this will make a "_build" directory in your given build directory. and will copy the binaries there. For more information try
cmake --help-command ADD_SUBDIRECTORY

Related

Visual Studio Solution Style using CMakelists [duplicate]

I have a CMake project that looks like this:
project/
CMakeLists.txt
subprojectA/
CMakeLists.txt
include/
headerA.hpp
src/
libraryA.cpp
subprojectB/
CMakeLists.txt
src/
mainB.cpp
The "library" subproject, A, is compiled as a static library, becoming libsubprojectA.a. The "main" project, B, is compiled as a binary and depends on the library. mainB.cpp includes a reference to headerA.hpp.
Here is subprojectA/CMakeLists.txt:
project(SubProjectA)
include_directories(include)
add_library(subprojectA STATIC src/libraryA.cpp)
set(${PROJECT_NAME}_INCLUDE_DIRS
${PROJECT_SOURCE_DIR}/include
CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
And here is subprojectB/CMakeLists.txt:
project(SubProjectB)
include_directories(${SubProjectA_INCLUDE_DIRS})
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)
The main Project CMakeLists.txt looks like:
project(Project)
add_subdirectory(subprojectB)
add_subdirectory(subprojectA)
Note that subprojectB, the main project, is listed before subprojectA.
Here's the problem. When I first run "cmake" on this project, ${SubProjectA_INCLUDE_DIRS} is not set within SubProjectB.
What I think is happening is that the CMakeLists for SubProjectB loads first, when ${SubProjectA_INCLUDE_DIRS} has not yet been set. It sets its own include path to an empty string as a result. However, even though libsubprojectA.a gets built successfully before mainBinary, the include path was already set empty beforehand. As a result, I get this error when trying to make mainBinary:
subprojectB/src/mainB.cpp:1:23: fatal error: headerA.hpp: No such file or directory
#include "headerA.hpp"
^
It's a workaround to put subprojectA before subprojectB in the main Project CMakeLists in the declarative world of CMake. What I really want is to know the proper way to indicate to CMake that the include_directories(${SubProjectA_INCLUDE_DIRS}) line depends on the definitions that exist inside SubProjectA's CMakeLists. Is there a better way to do this?
If you want to express that include directory subprojectA/include is an interface of the library subprojectA, attach this property to the target with target_include_directories command:
subprojectA/CMakeLists.txt:
project(SubProjectA)
add_library(subprojectA STATIC src/libraryA.cpp)
# PUBLIC adds both:
# 1) include directories for compile library and
# 2) include directories for library's interface
target_include_directories(subprojectA PUBLIC include)
So any executable(or other library) which linked with subprojectA will have this include directory automatically:
subprojectB/CMakeLists.txt:
project(SubProjectB)
add_executable(mainBinary src/mainB.cpp)
target_link_libraries(mainBinary subprojectA)
Of course, for use last command properly you need to process directory with library before one with executable:
CMakeLists.txt:
project(Project)
add_subdirectory(subprojectA)
add_subdirectory(subprojectB)

CMake Include Directories not found

I have a library project whose hierarchy looks something like this:
Root:
+ src/
+ apple.cpp
+ bananas/
+ bananas.cpp
...
+ include/
+ apple.h
+ bananas/
+ bananas.h
...
I've been using Visual Studio to compile it and it works fine but now I want to diversify it by allowing the library to be compiled using CMake. I've created the following CMakeLists.txt file (located in Root):
cmake_minimum_required(VERSION 2.8) # Version 2.8 required
file(GLOB_RECURSE HEADERS # Fetch all of the Lib's Headers
${CMAKE_CURRENT_SOURCE_DIR}/include/*.h
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
)
file(GLOB_RECURSE SOURCES # Fetch all of the Lib's Srcs
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
)
add_library( # Create the Library from Srcs
HoneycombGameEngine ${SOURCES}
)
target_include_directories( # Add the Headers to the Lib
HoneycombGameEngine
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
It compiles with CMake and generates its files, however when I run make to actually compile the library, I get an error in the bananas.cpp file (first file it chooses to compile) that it cannot find the bananas.h header file. If it is relevant, the bananas.cpp include header statement looks like this:
#include "..\..\include\bananas\bananas.h"
I'm not exactly sure what I'm doing wrong, this my first time using CMake so I mostly wrote my CMakeLists file using bits and examples from the internet. Anything that could point me in the right direction and help me solve this issue is much appreciated.
You are only get all .h inside include directory.
Bananas.h is inside bananas directory. Then you have to use something like it:
file(GLOB_RECURSE HEADERS # Fetch all of the Lib's Headers
${CMAKE_CURRENT_SOURCE_DIR}/include/*.h
${CMAKE_CURRENT_SOURCE_DIR}/include/bananas/*.h
)

Adding headers to Cmake

I am trying to run the examples from VTK and modify them to get to what I want to render on screen.
I am currently trying to add a server application running in parallel to the VTK rendering. I already have my code written for the server but I was wondering how I could add these headers and cpp to CMakeLists.txt.
Indeed, this is the CMakeLists.txt I have so far:
cmake_minimum_required(VERSION 2.8)
PROJECT(RotateActor)
option(INCLUDE_SERVER
"Use the server implementation" ON)
# add the Server library?
if (INCLUDE_SERVER)
include_directories({${CMAKE_CURRENT_SOURCE_DIR}/Server/})
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Server/tcp_server.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Server/tcp_server.h)
endif (INCLUDE_SERVER)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(RotateActor MACOSX_BUNDLE RotateActor ${SOURCES})
if(VTK_LIBRARIES)
target_link_libraries(RotateActor ${VTK_LIBRARIES})
else()
target_link_libraries(RotateActor vtkHybrid vtkWidgets)
endif()
I then generate using CMake and VS2012. When opening the sln file and trying to generate I get the following error so I'm guessing my integration of the headers is not correct.
C:\...\RotateActor.cxx(12): fatal error C1083: Impossible d'ouvrir le fichier include : 'tcp_server.h' : No such file or directory
I don't think you need the RotateActor.cxx file, but if you do let me know.
Thanks in advance for the help.
I see some problems in your CMake file. First of all, your *.h files musn't be given in the add_executable command. Try something like that :
cmake_minimum_required(VERSION 2.8)
project(RotateActor)
option(INCLUDE_SERVER "Use the server implementation" ON)
# Manage your libraries before your sources
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
# add the Server library ?
# Here, maybe use the path from the root, and not from the local dir ?
# Don't add the .h in the sources
if(INCLUDE_SERVER)
include_directories({${CMAKE_SOURCE_DIR}/Server})
set(RotateActor_CPP_SOURCES
${RotateActor_CPP_SOURCES}
${CMAKE_SOURCE_DIR}/Server/tcp_server.cpp
)
endif()
if(NOT VTK_LIBRARIES)
set(VTK_LIBRARIES vtkHybrid vtkWidgets)
endif()
add_executable(RotateActor ${RotateActor_CPP_SOURCES})
target_link_libraries(RotateActor ${VTK_LIBRARIES})

Building a library with an executable that uses it

I have a library "lib" and an executable "demo".
The issue I'm having is that "demo" can't get lib_INCLUDES and lib_LIBS lib variables, set by "lib". I want them set because in demo.h I do #include <lib.h> and it fails to find the include. I want CMake to make lib.h a global include, i.e. pass -I /path/to/the/dir/with/lib.h/ to the compiler.
Source tree:
- src/
- CMakeLists.txt
- lib/
- CMakeLists.txt
- lib.cpp
- lib.h
- demo/
- CMakeLists.txt
- demo.cpp
- demo.h
src/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(test)
add_subdirectory(lib)
add_subdirectory(demo)
src/lib/CMakeLists.txt
add_library(lib SHARED lib.h lib.cpp)
target_include_directories(lib ${CMAKE_CURRENT_SOURCE_DIR})
# defined for later use in src/demo/CMakeLists.txt
set(lib_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR})
set(lib_LIBS lib)
src/demo/CMakeLists.txt
include_directories(${lib_INCLUDES})
add_executable(demo demo.h demo.cpp)
target_link_libraries(demo ${lib_LIBS})
# empty! why?
message(STATUS ${lib_INCLUDES})
Tell me how "demo" can access lib_INCLUDES and lib_LIBS libraries set by "lib", they seem to be empty in src/demo/CMakeLists.txt.
I don't want src/demo/CMakeLists.txt contain paths to "lib", only src/lib/CMakeLists.txt should know details of "lib" and it should define nice variables for use in src/demo/CMakeLists.txt that hide all those details.
Please include a working CMakeLists.txt in your answer, if possible.
Please read carefully documentation of SET command. Your variable lib_INCLUDES has local scope to lib\CMakeLists.txt, so for example you can CACHE it.
Better way is to populate INTERFACE_INCLUDE_DIRECTORIES of lib by
target_include_directories(lib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
so the target_link_libraries automatically appends lib's include directories.

Can't compile executable with CMake

I started with the following directory structure:
project
exec
executable.exe
lib
src
include
config
<cmake-generated config file>
I created the library in the lib/src folder by using a CMakefile in the lib/src folder. The exe would compile.
Then, I moved my CMakeFile up to /lib, making sure to change the source file paths to /src/*
Now, when I try to compile, all my libraries compile and link fine, but when I try to link the executable, I get /usr/bin/ld: cannot find -lconfig.
Does anyone have any idea why this happens or how to fix it?
Here is some of my code:
./CMakeLists.txt:
include_directories(config)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
ADD_SUBDIRECTORY(libs) #library sources
ADD_SUBDIRECTORY(exec) #executable sources
CONFIGURE_FILE(${core_SOURCE_DIR}/config/config.h.in
${core_SOURCE_DIR}/config/config.h)
./libs/CMakeLists.txt:
file(GLOB src ...)
file(GLOB header ...)
add_library(lib ${src} ${header})
./exec/CMakeLists:
add_executable(executable executable.cpp)
link_directories(${core_SOURCE_DIR}/lib) #not sure if this is required
target_link_libraries(executable ${lots_of_libs})
Every library in lots_of_libs can be found as a .a file in the lib directory
One problem, probably not risolutive, is this:
link_directories(${core_SOURCE_DIR}/lib) #not sure if this is required
should be:
link_directories(${PROJECT_BINARY_DIR}/lib)
or:
link_directories(${LIBRARY_OUTPUT_PATH})
Anyway, normally you wouldn't need to add to your link_directories the path to a library that is built within the project, even if you have specified a different LIBRARY_OUTPUT_PATH

Resources