Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control subdirectory compiling order of cmake?

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.

like image 416
Yun Wen Avatar asked Sep 25 '15 11:09

Yun Wen


People also ask

What does Add_subdirectory do CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

Is CMake sequential?

This seems as a trivial question, since CMake is a script language the general answer is: strictly sequential.

What is Add_library in CMake?

add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [<source>...]) Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project.


2 Answers

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.

like image 112
arrowd Avatar answered Oct 01 '22 12:10

arrowd


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)
like image 32
FreeApe Avatar answered Oct 01 '22 13:10

FreeApe