tags:

views:

74

answers:

2

Hi,

I have 3 small dependent tools:

main-tool/
    tool1/
    tool2/ 

       * ----- main-tool ----- *
       |                       |
      tool1   ---------- >    tool2

The main-tool depends on tool1 & tool2.
The tool1 depends on tool2.
The CMakeFiles look like that:

main-tool/CMakeLists.txt
   SUBDIRS{"tool1"}
   SUBDIRS{"tool2"}

main-tool/tool1/CMakeLists.txt
   SUBDIRS("../tool2"}

I can compile tool1 smoothly. However whenever I want to compile main-tool the tool2 is included twice and produces error. How can I avoid this?

Thanks.

A: 

You really should use SUBDIRS only for sub directories. What you actually mean by dependency? If you link some libraries together use TARGET_LINK_LIBRARIES, CMake will find them in your project regardless directory. If it is run time dependency, you usually care about it only after installation or you can use "${CMAKE_CURRENT_BINARY_DIR}/../tool2/tool2" as path to the binary.

Michal Čihař
+1  A: 

First off, you should use add_subdirectory() instead of subdirs(), which is deprecated. You get more precise control of the order of processing that way.

Second, because everything depends on tool2, you should build tool2 first.

main-tool/CMakeLists.txt:
   add_subdirectory{"tool2"}
   add_subdirectory{"tool1"}

Don't add another add_subdirectory command in the tool1 directory. There are two approaches you can use to access tool2 from the tool1 build.

  1. First, you could define some cmake variables in the top level main CMakeLists.txt related to tool2, then populate those variables in the tool2 build. You might need to use the PARENT_SCOPE directive to set those variables from the tool2 subdirectory. Then use those variables to access tool2 from the tool1 subdirectory.
  2. Alternatively, you could build all three tools (tool2, tool1, and main tool, in that order) from the top level CMakeLists file. The build file can get long, but it saves headaches trying to manage the scope of cmake variables. This is the approach I usually take and I recommend it.

To summarize, my recommendation is "one big CMakeLists.txt file". Unless it gets really, really big.

Christopher Bruns
Thanks Christopher, I went with the (1) and used the configure_file(). I didn't know about the PARENT_SCOPE. That would help a lot.
name