0

I have a code repository like image below. I'm trying to add a new standalone executable. The main.cpp and CMakeLists.txt files are located in folder4 and main.cpp requires code from folder3.

At the moment I'm using:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Standalone)

# add the executable
add_executable(StandaloneExe main.cpp)

Should I now use file( GLOB SRCS *.cpp *.h ) to retrieve the headers and source files from folder3?

I just want the simplest way of generating this executable.

enter image description here

user997112
  • 29,025
  • 43
  • 182
  • 361

1 Answers1

1

Should I now use file( GLOB SRCS *.cpp *.h ) to retrieve the headers and source files from folder3?

No, you should never use GLOB to get sources. See my answer here for more detail: https://stackoverflow.com/a/65191951/2137996

I just want the simplest way of generating this executable.

Put your CMakeLists.txt in the root instead. Then just write:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Standalone)

# add the executable
add_executable(
  StandaloneExe
  folder2/folder4/main.cpp
  folder1/folder3/a.cpp
  folder1/folder3/b.cpp
)

# Might need this, maybe not, depending on your includes
target_include_directories(
  StandaloneExe
  PRIVATE 
    "${CMAKE_CURRENT_SOURCE_DIR}/folder1/folder3"
)

If you absolutely cannot move your lists file, then you can use absolute paths:

add_executable(
  StandaloneExe
  ${CMAKE_CURRENT_LIST_DIR}/../../folder2/folder4/main.cpp
  ${CMAKE_CURRENT_LIST_DIR}/../../folder1/folder3/a.cpp
  ${CMAKE_CURRENT_LIST_DIR}/../../folder1/folder3/b.cpp
)
Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • Thanks Alex. May I ask what is the condition on the includes? And I can't put the CMakaeLists in the root because there already is one there for another executable. This is just a small standalone for part of the system. – user997112 Feb 12 '21 at 23:36
  • If you had, eg. `#include ` in main.cpp, you might need to set the include path. – Alex Reinking Feb 12 '21 at 23:50
  • I've just tried target_include_directories and I get Cannot specify include directories for target ........ which is not built by this project – user997112 Feb 12 '21 at 23:53
  • Basically I'm trying to create a standalone executable which is going to run a small subset of the code base. That's why I wanted to locate the CMakeList and main.cpp within a subfolder – user997112 Feb 12 '21 at 23:55
  • @user997112 I can't say why that would happen without seeing your CMakeLists.txt, but it's likely just a typo. Or somehow you called it before add_executable – Alex Reinking Feb 13 '21 at 06:01