Adding header and .cpp files in a project built with cmake
You can put all header/source files in the same folder and use something like
file(GLOB SOURCES
header-folder/*.h
source-folder/*.cpp
)
add_executable(yourProj ${SOURCES})
In this way, you can do either of the following two methods to add new added header/source into VS:
- need to generate in CMake again.
- fake to edit the
CMakeLists.txt
a little bit, e.g. simply add a space. And then build your solution in VS, it will automatically add new header/source files.
How to properly add include directories with CMake
Two things must be done.
First add the directory to be included:
target_include_directories(test PRIVATE ${YOUR_DIRECTORY})
In case you are stuck with a very old CMake version (2.8.10 or older) without support for target_include_directories
, you can also use the legacy include_directories
instead:
include_directories(${YOUR_DIRECTORY})
Then you also must add the header files to the list of your source files for the current target, for instance:
set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_executable(test ${SOURCES})
This way, the header files will appear as dependencies in the Makefile, and also for example in the generated Visual Studio project, if you generate one.
How to use those header files for several targets:
set(HEADER_FILES ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_library(mylib libsrc.cpp ${HEADER_FILES})
target_include_directories(mylib PRIVATE ${YOUR_DIRECTORY})
add_executable(myexec execfile.cpp ${HEADER_FILES})
target_include_directories(myexec PRIVATE ${YOUR_DIRECTORY})
How to add new C++ header and source files in Qt Creator with CMake build?
Yes , As of version Qt Creator 4.10.2 . When you add new files it were not added to CMakeLists.txt by default. But you can make work around by using CMake file
command.
- Move your source and header files to a new subfolder ( sources )
- Edit your CMakeLists.txt to include that.
example)
file(GLOB SRCS "${CMAKE_SOURCE_DIR}/sources/*.cpp")
add_executable(MyTest ${SRCS}
CMake + Qt Creator: Add header files to project files
I add my header files always explicit to avoid any surprise.
But on MacOS using QtCreator 4.2.0 and cmake 3.7.1 I can't reproduce your issue.
However I recommend to use following structure to know which files are within project and to trigger update of cmake's data during update of CMakeLists.txt.
In project/CMakeLists.txt:
add_subdirectory(src)
include_directory(include)
add_executable(foo ${SRC_LIST})
In project/src/CMakeLists.txt:
set(SRC_LIST
${SRC_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/a.cpp
${CMAKE_CURRENT_SOURCE_DIR}/b.cpp
PARENT_SCOPE
)
How can i include header files from a directory into cmake
You need to tell CMake to add includes/
to your program's include path. Usually this is done with either include_directories
or target_include_directories
add_executable (HelloWorld ${source_files})
# Assuming this is meant to be a public directory
target_include_directories(HelloWorld PUBLIC "includes/")
How to separate header file and source file in CMake?
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)
How to write Cmake for multiple .cpp files and headers in different folders ?
Here is a simple example for CMake for a multiple files project. You will need to adapt it your own case:
|-- CMakeLists.txt <<---- cMAKEfile
|-- include
| \-- header.h
\-- src
|-- header.cpp
\-- main.cpp
Your CMakeLists.txt should look as follows:
project(test)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(test ${SOURCES})
Then you can execute cmake
and make
commands.
Related Topics
Undefined Symbols in Crypto++/iOS 64-Bit Project
Building Qt 4.5 with Visual C++ 2010
Import Nested Classes into Namespace - C++
How to Set the Background Image in Qt Stylesheet
How to Wait for Vsync in Xlib App
C++ Access to SQL Server from Linux
Determine How Many Times File Is Mapped into Memory
How to Do Password Authentication for a User Using Ldap
What Is The Linux Equivalent Of: Multibytetowidechar & Widechartomultibyte
Using Raii to Manage Resources from a C-Style API
Does the Alignas Specifier Work with 'New'
Best C++ Matrix Library for Sparse Unitary Matrices
Multithreading on Dual Core MAChine
How to Make Camera Follow a 3D Object in Opengl