First of all, it doesn't look like the CMake code you posted belongs to the top CMakeList.txt file, since it directly references .cpp files, and "projectA".
There are also a few things that are not idiomatic:
cmake_minimum_required(VERSION 3.2.2.)
why the trailing dot here ? Not sure whether cmake will complain, but it is useless either way.
set(CMAKE_BUILD_TYPE Debug)
Don't do that. Instead invoke cmake using the -DCMAKE_BUILD_TYPE=Debug
command line flag, or use ccmake, or a GUI, or edit the CMakeCache.txt file.
file(GLOB SOURCES "*.cpp")
Don't do that either. It is good practice to explicitely list source files in CMake, because globbing will not make cmake detect newly added or deleted files automatically. Some people might disagree though.
Now for the heart of the topic, here is how things are usually done:
Top CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2.2)
project(globalProject)
add_subdirectory(projectA)
add_subdirectory(projectB)
ProjectA CMakeLists.txt file (assuming projectA is an executable):
add_executable(projectA file1.cpp file2.cpp ... )
include_directories(${CMAKE_SOURCE_DIR}/projectB) # include files from ProjectB
target_link_libraries(projectA projectB)
install(TARGETS projectA RUNTIME DESTINATION bin)
ProjectB CMakeLists.txt file (assuming projectB is a library):
add_library(projectB file1.cpp file2.cpp ... )
install(TARGETS projectB
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
Please note that this setup needs the make install
step to create bin and lib at the location of your choice as given in the command line invocation of cmake -DCMAKE_INSTALL_PREFIX=..
(assuming make is run from the build dir).
Also, this setup lets you choose if you want to build static or shared libraries, by using: -DBUILD_SHARED_LIBS=ON
. To build both, you need to have two add_library
calls with two different names, one STATIC, the other SHARED.
To sum up the steps required to make this work:
$ mkdir build && cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=.. -DBUILD_SHARED_LIBS=ON
$ make
$ make install
You could put these commands in a script for reusability.
Now, your questions can be answered:
A) it is possible and recommended, see the code above, only one build directory is necessary.
B) See the code for the projectA/CMakeLists.txt, you have to call include_directories()
C) Yes it is possible. Each target in your project can be built individually from the build directory by using make and the target name: make projectB
and make projectA