1

I create software for each state. There is some state specific code, and some common code that runs on all the states. I have created the following directory structure:

   common
        foo
            CMakeLists.txt
        bar 
            CMakeLists.txt
    State
        AL
            bin
            src
                build
                CMakeLists.txt
                DMV 
                    CMakeLists.txt              
                ABC 
                    CMakeLists.txt
        AK
            bin
            src
                build
                CMakeLists.txt
                DMV 
                    CMakeLists.txt
                ABC 
                    CMakeLists.txt

CMakeList.txt in EACH state

set (COMMON "/home/me/common")
add_subdirectory(${COMMON}/foo {STATE_BIN_DIR})
add_subdirectory(${COMMON}/bar {STATE_BIN_DIR})
add_subdirectory(${DMV)

CMakeLists.txt in DMV directories (and ABC)

install(TARGETS dmv DESTINATION ${STATE_BIN_DIR})

CMakeLists.txt in Common foo directories (and bar)

install(TARGETS foo DESTINATION ${STATE_BIN_DIR})

cmake ERROR:

The binary directory
/home/me/State/AL/bin is already used to build a source directory.  It cannot be used to build source directory
/home/me/common/bar
Specify a unique binary directory name.

It works fine if I am only building foo. When I try to build bar outside the directory structure, it fails. What I don't understand is ALL my binaries (in state and common) are going to the State's bin directory.

reading this: CMAKE add sub-directory which is not sub-directory on real directory

It says "CMake cannot have two different source directories mapping into the same build directory."
But all the differnt source ( DMV, ABC) all go to bin. It works here.

CMake's site doesn't specify https://cmake.org/cmake/help/v3.0/command/add_subdirectory.html

Is there any work around to do what I want? I know I can create foo and bar drirectory under the States directory and just put a CMakeLisist.txt that points to the code in common.
That works. But I would like to keep the recipe with the code.

I think I can also create bin/foo and bin/bar to put those binaries in too. But some are shared libs that I would like in one place.

Any thoughts?

Thanks

Community
  • 1
  • 1
Tim Edwards
  • 97
  • 1
  • 2
  • 13
  • `But all the differnt source ( DMV, ABC) all go to bin.` - Do you mean that *deliverables* created under given source directories are **installed** to `bin`? If so, it is not prohibited. But same directory cannot used for **build** different source directories. And you cannot overcome that. – Tsyvarev May 10 '17 at 22:04
  • Thanks for your answer. yes, deliverables, not source, sorry. I misunderstood what _binary_dir_ meant. I just used the same directory as where the external source is and it work. Are there any pitfalls in doing what I am doing? – Tim Edwards May 11 '17 at 13:32
  • @TimEdwards Have you been able to overcome the issue? I am facing the same with an Android project... – ahasbini Sep 11 '17 at 11:21

1 Answers1

0

I have solved the issue by adding an extra sub-folder within the binary directory. It doesn't seem to cause any issues with my build and I've doubled checked the final build output (which is an APK in my case) if all the binaries are included and it did.

In your case just use add_subdirectory() as such:

add_subdirectory(${COMMON}/foo {STATE_BIN_DIR}/foo)
add_subdirectory(${COMMON}/bar {STATE_BIN_DIR}/bar)
...

Similar solution could be found here explained in details here: link

ahasbini
  • 6,761
  • 2
  • 29
  • 45