0

I have different projects (executables and libraries) that use the same internal libraries. Each project and internal library folder is located in the same base folder.

Let's say I have a shell tool named shelltool1 and shelltool2 that use lib1. They are located in

/path/to/base/shelltool1
/path/to/base/shelltool2
/path/to/base/lib1

Do I build the executable/library in the respective folders and link them in the CMakeLists.txt inside /path/to/base ?

So my idea would be

base: CMakeLists.txt

add_subdirectory(shelltool1)
add_subdirectory(shelltool2)
add_subdirectory(lib1)

target_link_libraries(shelltool1 lib1)
target_link_libraries(shelltool2 lib1)

shelltool1: CMakeLists.txt

add_executable(shelltool1 ${SRC})

shelltool2: CMakeLists.txt

add_executable(shelltool2 ${SRC})

lib1: CMakeLists.txt

add_library(lib1 ${SRC})

Is this sensible, or will I run into troubles?

J. D.
  • 113
  • 13
Philipp
  • 2,376
  • 5
  • 29
  • 47
  • Looks fine and like the most common approach to me. You could specify all the targets in the top level directory if you want to since that will allow removal of all nested CMake files but the approach you're using seems fine. – Arnav Borborah May 14 '18 at 12:29
  • 1
    Can be duplicate https://stackoverflow.com/questions/33534115/preferred-cmake-project-structure – Sergei Nikulov May 14 '18 at 12:37
  • 1
    my 2 cents, base/CMakeLists.txt should not contains target_link_libraries. prefer to put them next to the add_XXX and start by add_subdirectory(lib1), Currently in Sheeltool1/2/CMakeLists.txt you can't link with lib1 because CMake still doesn't know it until it enter in lib1 dir usually all variables are scoped by directory except for target which are available everywhere once declared thus entering first in lib1 then shelltoolN ... note: You should also decide if your target_link_lib are PRIVATE or PUBLIC ;) – Mizux May 15 '18 at 07:22
  • @MizuxDev So should I link inside the shelltool1/2 CMakeLists.txt? Maybe another question would be, is there a possibility to be able to build shelltool1 both from the base directory and from within shelltool1. Because right now I can only build it from the `base`. Trying to build `shelltool1` fails because `lib1` is not known. – Philipp May 15 '18 at 15:09
  • yup simply put the add_subdirectory(lib1) BEFORE other add_subdirectory. After the cool point is supposing you don't want to compile shelltool2 you simply have to comment the add_subdirectory(shelltool2) – Mizux May 15 '18 at 19:21

0 Answers0