I am getting an error with the cMakeLists.txt file below. The error is
Object library target "cm" may not link to anything.
The issue is with line target_link_libraries(cm corebase)
This builds OK on linux but I this error is occurring on windows.
I'd appreciate any ideas?
file(GLOB base
"*.cpp"
)
file (GLOB model
"*.cpp"
)
file(GLOB logger
"*.cpp"
)
file(GLOB pass
"*.cpp"
)
file(GLOB compiler
"src/compiler/*"
)
add_subdirectory(meta)
if (MSVC)
add_library(cm OBJECT ${compiler} ${pass})
add_library(corebase OBJECT ${base} ${logger})
add_library(model OBJECT ${model})
target_link_libraries(cm corebase)
add_dependencies(cm generate-comp-api)
add_dependencies(model generate-comp-api)
else()
add_library(cm SHARED ${base} ${logger} ${pass} ${compiler})
add_library(corebase SHARED ${base} ${logger})
add_library(model SHARED ${model})
target_link_libraries(cm corebase metamodel)
add_dependencies(cm generate-comp-api)
add_dependencies(model generate-comp-api)
add_subdirectory(python/api)
add_subdirectory(tests)
add_subdirectory(contrib/googletest)
#add_subdirectory(recordings)
endif(MSVC)
The error message is correct: OBJECT libraries shouldn't link to anything. Instead, one need to use object files, corresponded to the library, as source files for executable/other library:
# Create an OBJECT library
add_library(corebase OBJECT ${base} ${logger})
# And use its objects for other library.
add_library(cm OBJECT ${compiler} ${pass} $<TARGET_OBJECTS:corebase>)
Related
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'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
I'm creating a native library under Android Studio, and I'm hurting the following problem.
In my AndroidStudio project CMakeLists.txt, I have this:
cmake_minimum_required(VERSION 3.7.0)
add_library(native-lib
SHARED
src/main/cpp/native-lib.cpp )
IF(USE_EXTERNAL)
include(ExternalProject)
ExternalProject_Add(
project_mylib
DOWNLOAD_COMMAND ""
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/mylib/"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}
)
add_dependencies(native-lib project_mylib)
ELSE()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/mylib/)
ENDIF()
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries(native-lib
${log-lib}
mylib
)
A self-made library is located in src/main/cpp/mylib/, with a CMakeLists.txt:
cmake_minimum_required(VERSION 3.7.0)
project(lib)
add_library(mylib SHARED lib.cpp)
INSTALL(TARGETS mylib
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
When I use the "traditional" add_subdirectory(...) everything goes well. But, if I use the ExternalProject_Add(...) version, linker is skipping the compiled libmylib.so library and so cannot link mylib to native-lib.
I have the following message: skipping incompatible /home/.../app/.externalNativeBuild/cmake/debug/arm64-v8a/lib/libmylib.so when searching for -lmylib
My guess is that all the flags set by AndroidStudio for the root CMakeLists.txt are not set when the ExternalProject is compile leading to an incompatible shared library.
So, I wonder if there is a way to compile a cmake ExternalProject like it was part of the root project (sharing the same compile flags etc) ?
Thanks for any advice
I have the following code structure
Project->
project.cxx
CMakeLists.txt
meta->
CMakeLists.txt
first->
first.cxx
CMakeLists.txt
second->
second.cxx
CMakeLists.txt
where first/CMakeLists.txt contains:
FILE(GLOB first_sources "*.cxx")
ADD_LIBRARY(first OBJECT ${first_sources})
second/CMakeLists.txt contains:
FILE(GLOB second_sources "*.cxx")
ADD_LIBRARY(second OBJECT ${second_sources})
meta/CMakeLists.txt contains:
ADD_SUBDIRECTORY(first)
ADD_SUBDIRECTORY(second)
ADD_LIBRARY(meta OBJECT $<TARGET_OBJECTS:first> $<TARGET_OBJECTS:second>)
and finally, Project/CMakeLists.txt containts:
ADD_SUBDIRECTORY(meta)
FILE(GLOB Project_SOURCES "*.cxx")
ADD_LIBRARY(Project SHARED ${Project_SOURCES} $<TARGET_OBJECTS:meta>)
However,
ADD_LIBRARY(meta OBJECT $<TARGET_OBJECTS:first> $<TARGET_OBJECTS:second>)
fails given that OBJECT library expects c/cxx sources instead of object files.
What I am trying to achieve is a relocatable object file that combines both first.o and second.o into a meta.o as:
ld -r first.o second.o -o meta.o
What would be the CMake alternative for the same?
I have a cmake project where I am trying to link the executable to the shared library. But it is not getting linked. After searching enough and not finding any useful solutions, I'm posting my question here, please let me know if there are any obvious mistakes as I am not familiar with cmake
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
# Set Project Name
set(PROJECT_NAME myproj)
project(${PROJECT_NAME})
# Tell Cmake to invoke gcc with specific flags. Use c++11 standard.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Include all headers
include_directories(${PROJECT_SOURCE_DIR}/../public/include)
# Driver Program
file(GLOB_RECURSE SOURCE_FILES
"${PROJECT_SOURCE_DIR}/src/*.cpp"
)
# Dont know what to do about this
#set( CMAKE_SKIP_BUILD_RPATH true)
link_directories(${CMAKE_SOURCE_DIR}/../relative/path/to/sharedlib) #contains libshared.so
# Generate Executable, trying to generate shared library to see if it generates # correct dependencies
# add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
#Link the libraries
target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/../relative/path/to/sharedlib/libshared.so)
Here is the steps I have tried and some observations.
If I generate a shared library instead of an executable, it is able to find the dependent shared library(i.e libshared.so)
ldd myproj.so
libshared.so => full/path/to/sharedlib/libshared.so (0x00007fa26c263000)
I did not fully understand what it meant, but it is able to find the shared library is what I guessed.
If I try to generate an executable instead of shared library, I see undefined reference errors to the functions within the library.
I tried just target_link_libraries(${PROJECT_NAME} shared) as well and it gave same error.
Any clues as to how can I get the shared library linked to my executable? Thanks