0
project
  |------ CMakeLists.txt (The main Cmake)
  |------ somePlace/someOtherPlace/CmakeLists.txt
  |          |----- some.proto (google proto files)
  |          |----- CMakeList.txt
  |------ Project2/CmakeListst.txt
             |----- .cpp files
             |----- .hpp files
             |----- CMakeList.txt  

I have a similar topology as above, my main cmake could be able to generate cmake files, and after the main cmake I could be able to build Project2 with:

make Project2

I have added Project2 as subdirectory to the main cmake. I have no problem here. But I also want to build common/someplace/CmakeLists.txt together with the Project2, when I run make Project2. I also know that I could be able to build common/someplace/CmakeLists.txt inside its directory by cmake and make commands. You can check somePlace/someOtherPlace/CmakeLists.txt :

    INCLUDE(FindProtobuf)
    FIND_PACKAGE(Protobuf REQUIRED)
    INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
    PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER oamc_packets.proto)
    ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})

But I want Project2 to depend on somePlace/someOtherPlace/CmakeList.txt and build together with it.How can I achieve it?

Should I use add_executable/add_library commands? The problem is somePlace/someOtherPlace/CmakeLists.txt creates header and cc file along with the .a file.

PS: I can give further information, if it is requested.

Meric Ozcan
  • 678
  • 5
  • 25
  • 1
    Add `common/someplace/` as subdirectory? And accordingly add a dependency in `Project2` on `common/someplace`'s target – Alexey S. Larionov Oct 22 '21 at 12:52
  • Yes, I added somePlace/someOtherPlace/ as subdirectory. But how can I add a dependency in Project 2? @AlexeyLarionov – Meric Ozcan Oct 22 '21 at 12:53
  • 1
    The directory structure of your CMake files has relatively little impact on the resulting build system. Can you show which targets are defined where? Targets are defined by `add_executable` and `add_library` (or `add_custom_target` if you're doing something exotic...) – Botje Oct 22 '21 at 12:53
  • @Botje As AlexeyLarinov comment, cant I just add a dependency to my Project2? I believe it will do it for me, however, I couldn't find a proper example thats fits my case. I have couple nested sub-directories, because of this this example didn't do it for me: *https://stackoverflow.com/questions/4905089/how-do-i-correctly-create-dependencies-between-targets-in-cmake* – Meric Ozcan Oct 22 '21 at 12:57
  • 1
    You can add a dependency, but you need a target defined by either `add_executable`/`add_library`, as Botje suggested – Alexey S. Larionov Oct 22 '21 at 12:59
  • @AlexeyLarionov I am not sure if it is a executable or a library, it is Google Proto file, when it is built, it generates some headers and cc files. Becuase of this I couldn't be sure how to do it? – Meric Ozcan Oct 22 '21 at 13:01
  • Does it generate the files during configure stage or build stage? If the latter, I would expect some `add_custom_target` to be used by them atleast – Alexey S. Larionov Oct 22 '21 at 13:09
  • I added the CmakeFileLists.txt as above. – Meric Ozcan Oct 22 '21 at 13:23

1 Answers1

0

I peeked into the documentation of FindProtobuf, and they put a warning here

Note: The protobuf_generate_cpp and protobuf_generate_python functions and add_executable() or add_library() calls only work properly within the same directory.

So perhaps if you could include the content of somePlace/someOtherPlace/CmakeList.txt inside your Project2/CmakeListst.txt, then you could safely use the generated file.

I can't really test the following code, but maybe in Project2/CmakeListst.txt before using the generated files you can do

include("${CMAKE_CURRENT_SOURCE_DIR}/../somePlace/someOtherPlace/CmakeLists.txt")
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • Yes, Mr. Because of the note in the documentation, I am using cmake inside the somePlace/someOtherPlace/CmakeList.txt. I updated the question a little, the cmake file uses add_library as you said. I missed it. I will try you suggestion and update you – Meric Ozcan Oct 22 '21 at 13:38
  • The problem is how can I *make* somePlace/someOtherPlace/CmakeList.txt with the main CmakeList – Meric Ozcan Oct 22 '21 at 14:12