I'm trying to get mu C++ code to compile as object file (.o) but I can get it in (.a, .dylib, executable) forms
I've tried this answer: Copy out plain .o files with cmake
but didn't actually understood the solution and it didn't work either.
how can I achieve this ?
here is my CMake file:
cmake_minimum_required(VERSION 3.10)
project(myProject)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(LIBS_DIR ${PROJECT_SOURCE_DIR}/../libs)
file(GLOB SOURCES
src/*.cpp
)
find_library(SQLITE3
NAMES libsqlite3.0.tbd
)
MACRO(HEADER_DIRECTORIES return_list)
FILE(GLOB_RECURSE new_list ${LIBS_DIR}/*.h*)
SET(dir_list "")
FOREACH(file_path ${new_list})
GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
SET(dir_list ${dir_list} ${dir_path})
ENDFOREACH()
LIST(REMOVE_DUPLICATES dir_list)
SET(${return_list} ${dir_list})
ENDMACRO()
add_library(${PROJECT_NAME}_obj OBJECT ${SOURCES})
HEADER_DIRECTORIES(HDR_DIRS)
target_include_directories(${PROJECT_NAME}_obj PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/../fmt
${PROJECT_SOURCE_DIR}/../include
${HDR_DIRS}
)
target_link_libraries(${PROJECT_NAME}_obj ${SQLITE3})
You cannot merge multiple translation units into a single object file (is due to the features of the linker (ld util). only to library or elf.
Earlier gcc can to be able to merge several files into a single object file, but this feature was removed
Related
I want to build my C++ project with CMake and I want to include automatically every new file on "cmake ." my project structure is:
Application/ Graphics/ CMakeLists.txt CMakeLists.txt.user main.cpp
./Application:
CMakeLists.txt Logger/ Recovery/ application.cpp application.hpp firstclass.cpp firstclass.hpp singleton.hpp
./Application/Logger:
CMakeLists.txt logger.cpp logger.hpp
./Application/Recovery:
CMakeLists.txt recovery.cpp recovery.hpp
./Graphics:
CMakeLists.txt drawableobject.cpp drawableobject.hpp graphics.cpp graphics.hpp
Each folder has own CMakeLists.txt
I did so far this in master CMake:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Asteri VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
message("Source dir: ${SOURCE_DIR}")
#file(GLOB_RECURSE SRC_FILES ${SOURCE_DIR}/*.cpp)
#file(GLOB_RECURSE HEADER_FILES ${HEADER_DIR}/*.hpp)
set(PROJECT_NAME "Asteri")
macro(SUBDIRLIST result curdir)
file(GLOB children RELATIVE ${curdir} ${curdir}/*)
set(dirlist "")
foreach(child ${children})
if(IS_DIRECTORY ${curdir}/${child})
list(APPEND dirlist ${child})
endif()
endforeach()
set(${result} ${dirlist})
endmacro()
SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
foreach(subdir ${SUBDIRS})
message("Subdirectory: ${subdir}")
add_subdirectory(${subdir})
endforeach()
add_executable(${Asteri} main.cpp)
The question is how to connect all pieces together?
What I need in other CMakeLists.txt?
How to communicate children -> parent or I misunderstood the concept of CMake?
No need for other CMakeLists.
I guess you want something like this in top CMakeLists:
cmake_minimum_required(VERSION 3.12)
project(Asteri VERSION 1.0)
file(GLOB_RECURSE ASTERI_SRC_FILES CONFIGURE_DEPENDS
"${PROJECT_SOURCE_DIR}/Application/*.cpp"
"${PROJECT_SOURCE_DIR}/Graphics/*.cpp"
)
add_executable(Asteri main.cpp ${ASTERI_SRC_FILES})
It's worth noting that GLOB/GLOB_RECURSE -even with CONFIGURE_DEPENDS- is bad practice: it's slow, specially on Windows from my experience, and may not work as expected depending on generator used.
I found out the answer to my problems.
set(SOURCES "${PROJECT_DIR}/main.cpp" CACHE INTERNAL STRINGS)
add_subdirectory(application)
FOREACH(it ${SOURCES})
message("source file: ${it}")
ENDFOREACH()
add_executable(Asteri ${SOURCES})
In this way I store main.cpp path into variable SOURCES
You can see your variables after 'make' in CMakeCache.txt.
After this is done, my variable SOURCES looks like:
/STRINGS
SOURCES:INTERNAL=/home/default/cpp_testing_project/cmake_project/source/main.cpp;
After that in application subdirectory I have CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
# Get source files
file(GLOB CPP_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
set(SOURCES ${SOURCES} ${CPP_LIST} CACHE INTERNAL STRINGS)
And now SOURCES looks like:
SOURCES:INTERNAL=/home/default/cpp_testing_project/cmake_project/source/main.cpp;/home/default/cpp_testing_project/cmake_project/source/application/application.cpp;/home/default/cpp_testing_project/cmake_project/source/application/drawableobject.cpp;/home/default/cpp_testing_project/cmake_project/source/application/firstclass.cpp;/home/default/cpp_testing_project/cmake_project/source/application/graphics.cpp;/home/default/cpp_testing_project/cmake_project/source/application/application.hpp;/home/default/cpp_testing_project/cmake_project/source/application/drawableobject.hpp;/home/default/cpp_testing_project/cmake_project/source/application/firstclass.hpp;/home/default/cpp_testing_project/cmake_project/source/application/graphics.hpp;
I have a list of object files generated by a Makefile stored under say mylib directory. I am trying to link these object files while compiling one of the sub-directories in my project (I don't want to generate an executable). Here is my CMakeLists.txt file
cmake_minimum_required(VERSION 3.5.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g")
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
file(GLOB SOURCES "*.cpp" ".hpp")
include_directories(mylib)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/mylib)
add_library(slib SHARED ${SOURCES})
That is, mylib directory contains .h, .cc and .o files I generated after running make on mylib.
When I try to compile this, I get an Undefined symbols for architecture x86_64 error for mylib functions.
How can I link multiple precompiled object files generated by an external make? This question (how to add prebuilt object files to executable in cmake) gives a method to link a single object file. How do I do this for all the object files and generated a shared library instead of an executable?
I suggest to compile library "mylib" with ExternalProject (by direct call to gcc, for example) and, then use code like this:
add_library (slib SHARED ${SOURCES})
target_link_libraries (slib "mylib")
add_dependencies may be useful in some cases.
I am trying to port parts of an existing project to GPUs via CUDA code. I understand cmake has options (find_cuda...) to deal with .cu files separately, yet I am still trying to figure out how this ecosystem can be used in context of existing projects.
My question is the following. Let's say I have an existing C++ project with a cmake config file (CMakeLists). What is the current practice to eleganly (if possible) include CUDA kernels? Can CMakeLists be constructed in a way, .cu files are compiled only if GPU is present?
My current idea is to create a separate folder, where only CUDA related code exists and then compile this as a static library. Is that the way to do it?
Having the CUDA files in separate folders is my recommended way but not required. The basic principle is that you collect all .cu files in a CMake variable (let's call it CUDA_SRC) and all .cpp files in a different variable (call it SRC). Now you compile both files and put them together. The variable CUDA_FOUND provided by find_package(CUDA) can be used to determine if CUDA is installed on your system. The use of a static library for the cuda files is not required, but i'll show you both ways here.
In your top level cmake file you want to have something like this to find CUDA and set some nvcc flags:
find_package(CUDA QUIET)
if(CUDA_FOUND)
include_directories(${CUDA_INCLUDE_DIRS})
SET(ALL_CUDA_LIBS ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY} ${CUDA_cublas_LIBRARY})
SET(LIBS ${LIBS} ${ALL_CUDA_LIBS})
message(STATUS "CUDA_LIBRARIES: ${CUDA_INCLUDE_DIRS} ${ALL_CUDA_LIBS}")
set(CUDA_PROPAGATE_HOST_FLAGS ON)
set(CUDA_SEPARABLE_COMPILATION OFF)
list( APPEND CUDA_NVCC_FLAGS -gencode=arch=compute_30,code=compute_30 )
list( APPEND CUDA_NVCC_FLAGS -gencode=arch=compute_52,code=sm_52 )
endif()
With static CUDA library
if(CUDA_FOUND)
#collect CUDA files
FILE(GLOB_RECURSE CUDA_SRC *.cu)
#build static library
CUDA_ADD_LIBRARY(my_cuda_lib ${CUDA_SRC} STATIC)
SET(LIBS ${LIBS} ${my_cuda_lib})
endif()
#collect cpp files
FILE(GLOB_RECURSE SRC *.cpp)
#compile .cpp files and link it to all libraries
add_executable(${PROG_NAME} ${SRC})
target_link_libraries(${PROG_NAME} ${LIBS} )
Without Static CUDA lib
FILE(GLOB_RECURSE SRC *.cpp)
if(CUDA_FOUND)
#compile cuda files and add the compiled object files to your normal source files
FILE(GLOB_RECURSE CUDA_SRC *.cu)
cuda_compile(cuda_objs ${CUDA_SRC})
SET(SRC ${SRC} ${cuda_objs})
endif()
#compile .cpp files and link it to all libraries
add_executable(${PROG_NAME} ${SRC})
target_link_libraries(${PROG_NAME} ${LIBS} )
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 have a large, modular CMake project.
Some modules are static libraries, some are executables that may or may not use some of these libraries. I already have a system of requirement/dependency checking in place that would stop cmake and tell the user if he's trying to build an executable but not the library that it requires.
The project compiles fine as is. I would like to split this into subprojects, i.e. set project(${module_name}) for each of the modules' CMakeLists.txt. Unfortunately, as soon as I insert this line into the apppropriate macro, I immediately get linker errors, i.e. the executables can no longer see the symbols defined in the static libraries.
The c++ command (I happen to be using gcc 4.8) that is invoked on the executables is unchanged, so something else must be different. How can I fix this and retain the subproject structure?
EDIT: per #Gerald's comment, additional information.
Project folder structure:
root
|
|-bin
|-lib
|-cmake(various CMake scripts, added to module path)
|-third_party
|-...
|-CMakeLists.txt
|-modules(top-level module directory, expanded)
|
|-CMakeLists.txt
|-exec_module_a
|-exec_module_b
|-... (other exec modules)
|-lib_module_a
|-lib_module_b
|- ... (other lib modules)
|-lib_module_N (expanded)
|
|-CMakeLists.txt
|-include/root/module_N (various include files)
|-src (various source files)
|-tests
Structure of a typical leaf-level CMakeLists.txt (same but one arg. to root_add_subproject for both lib and exec modules):
include(RootUtils)
set(module_name ${EXEC_MODULE_A})
#---------------------------CHECK REQUIREMENTS---------------------------------#
root_check_module_requirements(module_name
BUILD_${LIB_MODULE_A}
BUILD_${LIB_MODULE_G}
#...
WITH_QT
WITH_THIRD_PARTY_BLAH1
WITH_THIRD_PARTY_BLAH2
#...
)
#---------------------------ADD SOURCES AND TARGET-----------------------------#
root_add_subproject(${module_name} executable qt_on)
#---------------------------ADD INCLUDES---------------------------------------#
target_include_directories(${TARGET_NAME} PUBLIC
#third-party includes
${THIRD_PARTY_BLAH1_INCLUDE_DIRS}
${THIRD_PARTY_BLAH1_INCLUDE_DIRS}
#...
#module includes
"${MODULES_DIR}/${LIB_MODULE_A}/include"
"${MODULES_DIR}/${LIB_MODULE_B}/include"
#...
)
#---------------------------LINK LIBRARIES-------------------------------------#
target_link_libraries(${TARGET_NAME} PUBLIC
#third-party libraries
${QT5_TARGETS}
${THIRD_PARTY_BLAH1_LIBRARIES}
${THIRD_PARTY_BLAH2_LIBRARIES}
#...
#module libraries
${LIB_MODULE_A}
${LIB_MODULE_B}
#...
)
if(UNIX)
#... link third-party dependencies conditional on platform
endif()
#---------------------------ADD TESTS------------------------------------------#
#SET(BUILD_TESTS FALSE)#comment out to build the local unit tests
root_add_tests(${module_name})
#==============================================================================#
The relevant script in RootUtils.cmake looks like this:
macro(augmentarium_add_subproject module_name proj_type qt_on)
if(${qt_on} STREQUAL "qt_on" OR ${qt_on} STREQUAL "YES" OR ${qt_on} STREQUAL "TRUE")
set(QT_ON TRUE)
else()
set(QT_ON FALSE)
endif()
#---------------------------DEFINE SUBPROJECT----------------------------------#
#TODO: fix problems with macro (figure out how to set up library as a subproject w/o destroying linking capability)
#project(${module_name}) # <------- THIS is the line that causes linker errors
set(TARGET_NAME ${module_name})
string(TOUPPER "${TARGET_NAME}" TARGET_NAME_UPPER)
if(QT_ON)
set(CMAKE_AUTOMOC ON)
endif()
#---------------------------DEFINE SOURCE FILES--------------------------------#
include_directories(include)
SET(${TARGET_NAME_UPPER}_TOP_INCLUDE_DIR include/${PROJECT_NAME}/${TARGET_NAME})
file(GLOB ${TARGET_NAME_UPPER}_CMAKELISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt)
file(GLOB ${TARGET_NAME_UPPER}_SOURCES src/*.cpp)
file(GLOB ${TARGET_NAME_UPPER}_HEADERS src/*.h src/*.h.in src/*.hpp src/*.tpp
${${TARGET_NAME_UPPER}_TOP_INCLUDE_DIR}/*.h
${${TARGET_NAME_UPPER}_TOP_INCLUDE_DIR}/*.h.in
${${TARGET_NAME_UPPER}_TOP_INCLUDE_DIR}/*.hpp
${${TARGET_NAME_UPPER}_TOP_INCLUDE_DIR}/*.tpp)
file(GLOB ${TARGET_NAME_UPPER}_TEST_SOURCES tests/*.cpp)
if(QT_ON)
file(GLOB_RECURSE MOC_${TARGET_NAME_UPPER}_SOURCES moc_*.cpp *_automoc.cpp qrc_*.cpp)
#remove generated moc files
foreach(FILE_NAME ${MOC_${TARGET_NAME_UPPER}_SOURCES})
list(REMOVE_ITEM ${TARGET_NAME_UPPER}_SOURCES ${FILE_NAME})
endforeach()
endif(QT_ON)
if(QT_ON)
#---------------------------ADD UI FILES---------------------------------------#
file(GLOB ${TARGET_NAME_UPPER}_UI src/*.ui)
if(BUILD_${TARGET_NAME})
#this macro doesn't get defined unless QtWidgets is found
qt5_wrap_ui(${TARGET_NAME_UPPER}_UI_HEADERS ${${TARGET_NAME_UPPER}_UI})
endif()
#---------------------------ADD RESOUCE FILES----------------------------------#
file(GLOB ${TARGET_NAME_UPPER}_RESOURCE_FILES *.qrc)
if(BUILD_${TARGET_NAME})
#this macro doesn't get defined unless QtWidgets is found
qt5_add_resources(${TARGET_NAME_UPPER}_GENERATED_RESOURCES ${${TARGET_NAME_UPPER}_RESOURCE_FILES})
endif()
endif(QT_ON)
#..........................organize source/header files........................#
source_group("Source Files" FILES ${${TARGET_NAME_UPPER}_SOURCES})
source_group("Header Files" FILES ${${TARGET_NAME_UPPER}_HEADERS})
if(QT_ON)
source_group("Resource Files" FILES ${${TARGET_NAME_UPPER}_RESOURCE_FILES})
source_group("UI Files" FILES ${${TARGET_NAME_UPPER}_UI})
source_group("Generated Files" FILES ${${TARGET_NAME_UPPER}_GENERATED_RESOURCES} ${${TARGET_NAME_UPPER}_UI_HEADERS})
endif()
#---------------------------ADD TARGET-----------------------------------------#
set(ALL_${TARGET_NAME_UPPER}_FILES
${${TARGET_NAME_UPPER}_CMAKELISTS}
${${TARGET_NAME_UPPER}_SOURCES}
${${TARGET_NAME_UPPER}_HEADERS}
${${TARGET_NAME_UPPER}_RESOURCE_FILES}
${${TARGET_NAME_UPPER}_UI}
${${TARGET_NAME_UPPER}_GENERATED_RESOURCES}
)
if(${proj_type} STREQUAL "executable")
add_executable(${TARGET_NAME} ${ALL_${TARGET_NAME_UPPER}_FILES})
elseif(${proj_type} STREQUAL "library")
add_library(${TARGET_NAME} STATIC ${ALL_${TARGET_NAME_UPPER}_FILES})
else()
add_custom_target(${TARGET_NAME} ${ALL_${TARGET_NAME_UPPER}_FILES})
endif()
endmacro()
If that's still not enough, here's how the subprojects are included in the module-level CMakeLists.txt:
foreach (MODULE_NAME ${MODULES})
add_subdirectory (${MODULE_NAME})
endforeach()
CMake version used: 3.1.3
How do you manage dependencies between libs with CMake? This should be done using TARGET_LINK_LIBRARIES(myexe mylib1 mylib2)