My project is in the structure as follows:
--root: main.cpp CMakeLists.txt
--src: function.cpp CMakeLists.txt
--include: function.h
main.cpp:
#include <iostream>
#include "function.h"
using namespace std;
int main(int argc, char *argv[])
{
//call module in function.hpp
return 0;
}
CMakeLists.txt in root directory:
project(t1)
cmake_minimum_required(VERSION 2.8)
add_subdirectory(src)
file(GLOB_RECURSE SOURCES
include/function.h
src/function.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
CmakeLists.txt in src directory:
include_directories(${PROJECT_SOURCE_DIR}/include)
How to write CMakelists in the root and src directory to realize separate implementation of function? And further more, how to call them in main. Possible solutions in CMake not finding proper header/include files in include_directories. But it still doesn't match my situations.
To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .
The source directory is where the source code for the project is located. This is also where the CMakeLists files will be found. The binary directory is sometimes referred to as the build directory and is where CMake will put the resulting object files, libraries, and executables.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.
in root:
project(t1)
cmake_minimum_required(VERSION 2.8)
include_directories(include)
add_subdirectory(src)
in src:
set(TARGET target_name)
add_executable(${TARGET} main.cpp function.cpp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With