My problem is when I run $ cmake /path/to/source/ the resulting files and directories are split between the directory I'm calling from and /path/to/source/include.
Here is my CMake project:
File structure:
root:
|-CMakeLists.txt
|-src
| |-CMakeLists.txt
| |-"source files"
|-include # This is where part of the generated files are ending up.
| |-CMakeLists.txt
| |-"include files"
Here are my CMakeLists.txt's:
root/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(DUCKSIM)
# Add the root directory for the CMakeLists.txt being called. This is necessary for
# out-of-tree builds.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Add the directories containing source and header files.
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/include)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY $(pwd)) # This is an attempt to fix my problem
src/CMakeLists.txt:
# set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set DUCKSIM_SOURCES as all .cpp files
file(GLOB DUCKSIM_SOURCES *.cpp)
# Set the name of the executable as "ducksim" and link it with main.cpp
# and every thing in the DUCKSIM_SOURCES variable.
add_executable(ducksim main.cpp ${DUCKSIM_SOURCES})
include/CMakeLists.txt:
# set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set DUCKSIM_SOURCES as all .h files
file(GLOB DUCKSIM_SOURCES *.h)
Command add_subdirectory() will optionally accept a second path as a parameter, but that will indicate the binary directory, as indicated in the documentation. By giving your include folder as the second parameter, CMake assumes that you want the binaries to go there. That's why you end up with some files at the top level (CMakeCache.txt, etc.) and some files in the include folder.
For the record, using file(GLOB ...) is not recommended for collecting source files to compile. If you add a source file, no CMake file will have changed, and the build system won't regenerate.
Finally, you shouldn't need the file(GLOB ...) for the header files, but you probably need an include_directories() call for the include folder.
Related
I used CMake to compile a framework into three different .lib files, and one .sln file. The project is structured as such
UeiDaqFramework
|-CMakeLists.txt
|-build
| |-Framework_static.sln
| | - other cmake files and stuff
|-UeiDaqCore
| |-CMakeLists.txt
| |-source code for this subproject
|-UeiSimuDriver
| |-CMakeLists.txt
| |-source code for this subproject
|-UeiPDNADriver
| |-CMakeLists.txt
| |-source code for this subproject
|
|-Output
|-UeiDaqCore.lib
|-UeiSimuDriver.lib
|-UeiPDNADriver.lib
My test code require a Framework_static.lib in its include file instead of a .sln file, and I do not know how to combine the three existing .lib files using cmake. I did attempt to follow what this CMake - combine multiple libraries into one answer suggested, by just building all the source files into a single library, but I'm new to CMake, and it did not work. Here is the CmakeLists.txt file at the top directory of my project.
cmake_minimum_required(VERSION 3.13.0 FATAL_ERROR)
set(CMAKE_SYSTEM_VERSION CACHE TYPE INTERNAL FORCE)
project(Framework_static_vc15)
################################################################################
# Set target arch type if empty. Visual studio solution generator provides it.
################################################################################
if(NOT CMAKE_VS_PLATFORM_NAME)
set(CMAKE_VS_PLATFORM_NAME "x64")
endif()
message("${CMAKE_VS_PLATFORM_NAME} architecture in use")
if(NOT ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32"))
message(FATAL_ERROR "${CMAKE_VS_PLATFORM_NAME} arch is not supported!")
endif()
################################################################################
# Global configuration types
################################################################################
set(CMAKE_CONFIGURATION_TYPES
"Debug"
"Release"
CACHE STRING "" FORCE
)
################################################################################
# Global compiler options
################################################################################
if(MSVC)
# remove default flags provided with CMake for MSVC
set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
endif()
################################################################################
# Global linker options
################################################################################
if(MSVC)
# remove default flags provided with CMake for MSVC
set(CMAKE_EXE_LINKER_FLAGS "")
set(CMAKE_MODULE_LINKER_FLAGS "")
set(CMAKE_SHARED_LINKER_FLAGS "")
set(CMAKE_STATIC_LINKER_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_DEBUG "${CMAKE_STATIC_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS}")
endif()
################################################################################
# Nuget packages function stub.
################################################################################
function(use_package TARGET PACKAGE VERSION)
message(WARNING "No implementation of use_package. Create yours.")
endfunction()
################################################################################
# Common utils
################################################################################
include(CMake/Utils.cmake)
################################################################################
# Additional Global Settings(add specific info there)
################################################################################
include(CMake/GlobalSettingsInclude.cmake OPTIONAL)
################################################################################
# Use solution folders feature
################################################################################
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
################################################################################
# Sub-projects
################################################################################
add_subdirectory(Source/UeiDaqCore)
add_subdirectory(Source/UeiPDNADriver)
add_subdirectory(Source/UeiSimuDriver)
These CMakeLists.txt files were generated automatically by a generator that takes a visual studio .sln file and converts it to a .txt tree. Heres the link to the converter if anyone is interested. https://cmakeconverter.readthedocs.io/en/develop/intro.html
Im not sure if I need to rewrite Just the top level .txt file, or if I need to rewrite all the CMakeLists.txt files. Any help would be appreciated.
I assume the build of the shared libs is within the subdirectories, because your CMakeLists.txt does not contain any add_library command.
To get a static library containing all sources you have to
Collect all sources
Add command to build the static library
For 1. add the following to every subdir/CMakeLists.txt
set(SOURCES # This creates a list of sources called SOURCES
myFileA.cpp # This adds an item to the list
myFileB.cpp
${SOURCES} # This adds all items of existing list SOURCES to the current list
PARENT_SCOPE # This makes SOURCES visible in the parent CMakeLists.txt
)
For 2. add the static library
add_library(myTestLib_static.c STATIC ${SOURCES})
Well, I am new to CMake.
I have the following file structure
haze_removal
|---build
|---bin
| |--Test
| |--CMakeLists.txt
|---lib
| |--libtools.so
|---include
| |--tools.hpp
|---test
| |--main.cpp
| |--CMakeLists.txt
|---src
| |--tools.cpp
| |--CMakeLists.txt
|---CMakeLists.txt
The libtools.so is builded from ../src/tools.cpp. I build the whole project in ../build using the following cmake command:
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
The Test is build from ../test/main.cpp
I build this project successfully. But when I debug Test using gdb ./Test, I can't skip in the function that from libtools.so.
These are my CMakeLists.txt from different directories.
CMakeLists.txt under haze_removal/
cmake_minimum_required(VERSION 2.8)
project(haze_removal)
# find needed package
find_package(OpenCV REQUIRED)
# library directory
add_subdirectory(src)
# test
add_subdirectory(test)
CMakeLists.txt under ../src/
# generate dynamic library
# add source file, include directories
aux_source_directory(. TOOLS_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
# generate
add_library(tools SHARED ${TOOLS_SRC})
# set output directory and lib's name
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set_target_properties(tools PROPERTIES OUTPUT_NAME "tools")
# link library
target_link_libraries(tools ${OpenCV_LIBS})
CMakeLists.txt under ../test/
# add source file, include directories, link directories
aux_source_directory(. EXE_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
# generate
add_executable(Test ${EXE_SRC})
# set output directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# link libraries
target_link_libraries(Test ${OpenCV_LIBS} libtools.so)
My question is how I can debug the functions that from libtools.
Well, I find that why I can't step in the dynamic library even though I set CMAKE_BUILD_TYPE=Debug. Because before I set the build model to Debug I used to set CMAKE_BUILD_TYPE=Release. After I change the build model, I didn't delete the files in the build directory.
I don't know whether the above explanation is right or not, but I do solve my problem.
Thx!
I have seen a lot of similar questions and answers, but until now it seems not so obvious to get it working. I am quite new to CMake and until now everything was easy except the integration with protocol buffers.
I have a project with subdirectories, where each subdirectory has its own CMakeLists.txt
One of the subdirectory contains a .proto file. If the PROTOBUF_GENERATE_CPP macro is executed it generates the sources and the headers files. This macro is invoked from the CMakeLists.txt in the subdirectory containing the .proto file.
It seems however the make file is not invoked because no sources are added to the target. I can not add the sources to the target, because the files do not exist, they exist after generation, so this results in an error when CMake runs.
Setting the file properties to generated seems also not to help. In general, before the build process starts the macro should have been run to generated the source files.
How to do this, any working examples ?
Example:
./src/externals/protodef (from other repository, only contains .proto files)
./src/generated (supposed for the generated c and header files by protoc)
CMakeLists-1 (project root)
cmake_minimum_required (VERSION 2.6)
PROJECT (prototest)
ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/src/externals/protodef")
ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/src")
SET_SOURCE_FILES_PROPERTIES(${PROTO_SOURCES} ${PROTO_HEADERS} PROPERTIES GENERATED TRUE)
ADD_EXECUTABLE(prototest ${PROTO_SOURCES} ${SOURCE} )
TARGET_LINK_LIBRARIES(prototest ${EXTERNAL_LIBS} )
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
CMakeLists-2 (src)
SET(SOURCE ${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
PARENT_SCOPE
)
CMakeLists-3 (src/externals/protodef)
SET(PROTOBUF_PATH "D:/protobuf-3.0.0/" )
SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${PROTOBUF_PATH}")
# Changing PROTO_SRCS and PROTO_HDRS does not work for setting the location
# of the generated files.
# Those variable are ignored by CMake for compiling the proto files.
# Using a dedicated CMakeLists.txt and settng CURRENT_BINARY dir is a
# workaround to get them where we want.
SET(GENERATED_DIR ${PROJECT_SOURCE_DIR}/src/generated )
SET(CMAKE_CURRENT_BINARY_DIR ${GENERATED_DIR} )
INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS${CMAKE_CURRENT_SOURCE_DIR}/test1.proto)
SET( EXTERNAL_LIBS ${PROTOBUF_PATH}/lib/libprotobuf.a PARENT_SCOPE)
# Propagate sources to the parant project
SET(PROTO_SOURCES ${PROTO_SRCS}
PARENT_SCOPE
)
SET(PROTO_HEADERS ${PROTO_HDRS}
PARENT_SCOPE
)
First generate the protobuf files, then add them to a CMake target.
CMakeLists (src) :
# Generate h/cpp proto files (./src/externals/protodef) into ./src/generated folder
PROTOBUF_GENERATE_CPP(...)
# Process subdir
ADD_SUBDIRECTORY(generated)
It seems that PROTOBUF_GENERATE_CPP can only be used in the same subdirectory. A possible workaround is to invoke protoc directly instead :
FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/externals/protodef PROTOMODEL_PATH)
FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src/generated PROTOBINDING_PATH)
FILE(GLOB DATAMODEL_PROTOS "${CMAKE_CURRENT_SOURCE_DIR}/externals/protodef/*.proto")
FOREACH(proto ${DATAMODEL_PROTOS})
FILE(TO_NATIVE_PATH ${proto} proto_native)
EXECUTE_PROCESS(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${PROTOMODEL_PATH} --cpp_out=${PROTOBINDING_PATH} ${proto_native}
RESULT_VARIABLE rv)
# Optional, but that can show the user if something have gone wrong with the proto generation
IF(${rv})
MESSAGE("Generation of data model returned ${rv} for proto ${proto_native}")
ENDIF()
ENDFOREACH(proto)
CMakeLists (src/generated) :
## List generated sources files
FILE(GLOB HDRS "*.h")
FILE(GLOB SRCS "*.cc")
ADD_LIBRARY(protoBinding ${HDRS} ${SRCS})
# ${PROTOBUF_LIBRARIES} should be defined by FIND_PACKAGE(Protobuf REQUIRED)
TARGET_LINK_LIBRARIES(protoBinding ${PROTOBUF_LIBRARIES})
This way CMake will first generate the header/source files, and only then add the generated files to a CMake target.
You can then use protoBinding target to link the generated files to an other target (e.g at the end of src's CMakeLists.txt) :
ADD_LIBRARY(myModel ${myFiles})
TARGET_LINK_LIBRARIES(myModel protoBinding)
I'm trying to use GLOB_RECURSE to specify my sources and headers files. Currently, my CMakeLists.txt for a static library is:
project(LinearSystemLib)
file(GLOB_RECURSE ${PROJECT_NAME}_headers ${PROJECT_SOURCE_DIR}/*.h)
file(GLOB_RECURSE ${PROJECT_NAME}_sources ${PROJECT_SOURCE_DIR}/*.cpp)
add_library(
${PROJECT_NAME} STATIC ${${PROJECT_NAME}_headers}
${${PROJECT_NAME}_sources}
)
install(
TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION libs
ARCHIVE DESTINATION archives
)
The library directory looks like this:
LinearSystemLib
CMakeLists.txt
source
LinearSystemLib.cpp
include
LinearSystemLib.h
When I run command cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug (in the build directory) everything goes ok. Yet, command make it displays the following:
/home/felipe/Documents/Dados/SINMEC/Eclipse/LinearSystemLib/source/LinearSystemLib.cpp:1:29: fatal error: LinearSystemLib.h: No such file or directory
Is my CMakeLists wrong? I don't want to set specify the sources and headers files by name. I'm not finding information about glob_recurse easily.
I can make it work by listing the sources and headers files by name. However, it MUST be done with the glob_recurse or with glob.
I solved my problem, here's what LinearSystemLib directory looks like:
LinearSystemLib
CMakeLists.txt
source
LinearSystemLib.cpp
include
LinearSystemLib.h
The CMakeLists.txt contains:
project(LinearSystemLib)
#INCLUDE DIRECTORIES
include_directories(${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/include)
#SEARCH FOR .CPP AND .H FILES
file(GLOB ${PROJECT_NAME}_headers ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/include/*.h)
file(GLOB ${PROJECT_NAME}_sources ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/source/*.cpp)
#ADD LIBRARY
add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_sources})
#DEFINE OUTPUT LOCATION
install(
TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION static_libs
)
You don't actually NEED to add the .h/.hpp files using GLOB. I did it because otherwise, Visual Studio (or CodeBlocks) wouldn't create a "Header Files" folder on the project menu.
I wasn't properly specifying the path where GLOB would find the files.
${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/source/
You need to add
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
so the correct -I argument is added to your compilation step. Use make VERBOSE=1 to see exactly what commands make is executing.
Consider the following setup:
-project
--src
---CMakeLists.txt
---main.cpp
---Application.cpp
---Application.hpp
---subfolder
----SomeClass.cpp
----SomeClass.hpp
--bin
And consider this CMakeLists.txt
project(SampleProject)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
# Include directories
INCLUDE_DIRECTORIES("subfolder")
# Executable
add_executable(${PROJECT_NAME} ${SRC_LIST})
Now, as long as I had all my classes in the same folder (src) everything worked perfectly fine.
But now I want to restructure my application a bit. I want to build a folder-hierarchy that represents the namespaces.
Of course in my includes I would then use
#include "subfolder/SomeClass.hpp"
but it doesn't work that way. I had a look at the manpage but there are so many options in CMake and it's often talking about standalone libraries that have their own CMakeLists.txt... I'm not that far yet. I just want to add a subfolder, that's all.
Until now I've used QMake for my C++ projects, but I wanted to dive into CMake now.
Are there any useful tutorials out there? I've found a few, but they they don't cover the basics.
The recommened way is to use a CMakeLists.txt for every subdirectory in existence. If you want to have subdirectories and organize them without having to create multiple CMakeLists.txt files, you can create one in the main directory with those contents:
project(SampleProject)
cmake_minimum_required(VERSION 2.8)
include_directories(src)
file(GLOB_RECURSE SRC_LIST *.c* *.h*)
# Executable
add_executable(${PROJECT_NAME} ${SRC_LIST})
Using aux_source_directory is used mainly for template related things. Also it's common practice to use a toplevel CMakeLists.txt, which includes the further files, has common project settings etc.:
<project>
|
+- CMakeLists.txt
|
+- src/
|
+-- CMakeLists.txt
|
+-- main.cpp
|
…
So this would look like:
CMakeLists.txt (Project dir):
project(SampleProject)
cmake_minimum_required(VERSION 2.8)
include_directories(src) # Add 'src' to include paths
subdirs(src) # Includes the 'src' directory and its cmake file
# ...
Now you can use the include path as expected.
CMakeLists.txt (src dir):
# Better add src files this way:
add_executable(${PROJECT_NAME} main.cpp Application.cpp)
subdirs(subfolder) # TODO: handle subfolder
The subfolder can be added through an additional library target, that is linked to your executable. Usually there's another CMakeLists.txt file in there.
Also make sure your cmake cache is updated; best you recreate it.