How to control subdirectory compiling order of cmake?

Yun Wen picture Yun Wen · Sep 25, 2015 · Viewed 11.3k times · Source

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.

Answer

arrowd picture arrowd · Sep 25, 2015

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.