I downloaded the sources of Qt and compiled it with -static flag into /usr/local/Qt5_static/. I created empty project and changed CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.6)
project(Splines)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -static -stdlib=libc++ -lc++abi -v")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set(SOURCE_FILES main.cpp)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(Qt5_DIR "/usr/local/Qt5_static/")
find_package(Qt5Widgets)
add_executable(Splines ${SOURCE_FILES})
target_link_libraries(Splines Qt5::Widgets)
There is a linker error.
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/libQt5Widgets.so.5.7.0'
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like linker tried to link shared lib instead of static lib i compiled. I tried other variations of CMakeLists.txt but failed.
How to statically link Qt with cmake properly?
(Sorry for my bad english)
CMAKE_CXX_FLAGS are compiler flags, but target_link_libraries uses linker flags, which are CMAKE_EXE_LINKER_FLAGS. For static link you need to add -static to linker flags.
Related
I am trying to link eigen to my project on clion, but the following error is printed:
This is my cmakelist file with all the attempts to link the library:
cmake_minimum_required(VERSION 2.8.3)
project(planner_standalone_grasp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-diagnostics-color")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
# find all cpp files in currect directory (where CMakeLists.txt is)
file(GLOB SOURCE_FILES FILES_MATCHING PATTERN "./src/*.cpp")
include_directories(src)
add_executable(${PROJECT_NAME} main.cpp ${SOURCE_FILES})
set(EXE_LIBS
ctop_common
ctop_log
ctop_util
eigen
crl
crl-algorithm
crl-loader
crl-tsplib
yaml-cpp
)
target_link_directories(${PROJECT_NAME} PUBLIC lib/comrob/lib/)
target_link_directories(${PROJECT_NAME} PUBLIC ctop/common/cmake-build-debug/)
target_link_directories(${PROJECT_NAME} PUBLIC ctop/log/cmake-build-debug/)
target_link_directories(${PROJECT_NAME} PUBLIC ctop/util/cmake-build-debug/)
set(EIGEN_DIR "/usr/local/include/Eigen/")
include_directories(${EIGEN_DIR})
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
target_link_libraries (${PROJECT_NAME} Eigen3::Eigen)
target_link_directories(${PROJECT_NAME} PUBLIC lib/eigen/Eigen/)
target_link_directories(${PROJECT_NAME} PUBLIC /usr/local/include/Eigen/)
target_link_directories(${PROJECT_NAME} PUBLIC /usr/local/include/eigen3/Eigen/)
target_link_libraries(${PROJECT_NAME} ${EXE_LIBS})
All other libraries in the project are successfully linked except Eigen - This is the error message:
[6/6] Linking CXX executable planner_standalone_grasp
FAILED: planner_standalone_grasp
: && /usr/bin/c++ -O0 -fno-diagnostics-color -std=c++17 -g -rdynamic CMakeFiles/planner_standalone_grasp.dir/main.cpp.o CMakeFiles/planner_standalone_grasp.dir/src/Grasp.cpp.o CMakeFiles/planner_standalone_grasp.dir/src/WallEdge.cpp.o CMakeFiles/planner_standalone_grasp.dir/src/WallGraph.cpp.o CMakeFiles/planner_standalone_grasp.dir/src/pt2eigen.cpp.o -o planner_standalone_grasp -L/home/sim/CLionProjects/Test/lib/comrob/lib -L/home/sim/CLionProjects/Test/ctop/common/cmake-build-debug -L/home/sim/CLionProjects/Test/ctop/log/cmake-build-debug -L/home/sim/CLionProjects/Test/ctop/util/cmake-build-debug -L/home/sim/CLionProjects/Test/lib/eigen/Eigen -L/usr/local/include/Eigen -L/usr/local/include/eigen3/Eigen -Wl,-rpath,/home/sim/CLionProjects/Test/lib/comrob/lib:/home/sim/CLionProjects/Test/ctop/common/cmake-build-debug:/home/sim/CLionProjects/Test/ctop/log/cmake-build-debug:/home/sim/CLionProjects/Test/ctop/util/cmake-build-debug:/home/sim/CLionProjects/Test/lib/eigen/Eigen:/usr/local/include/Eigen:/usr/local/include/eigen3/Eigen -lctop_common -lctop_log -lctop_util -leigen -lcrl -lcrl-algorithm -lcrl-loader -lcrl-tsplib -lyaml-cpp && :
/usr/bin/ld: cannot find -leigen
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Any help would be highly appreciated!
In my experience there is two possible causes for this issue:
there is no library named libeigen. in the provided directories (the ones in target_link_directories). Perhaps the path is wrong?
You should know that usually library files are not located in the include directories of a repository...
there is a library named as such, though it was compiled for the wrong platform... i.e. a library compiled for linux cannot be linked if you are making an executable for windows. Usually you can find the source code and compile it yourself.
Hope this helps.
I get a message 'can not be used when making a shared object; recompile with -fPIC'
I have try other examples and the issue is the same.
I have try
changing from MODULE to SHARED
cmake .. -DCMAKE_CXX_FLAGS=-fPIC
and other variations
this works:
c++ -c -fPIC -I/usr/include/python3.6m ../account.cpp
c++ -shared -Wall -Werror -Wl,--export-dynamic account.o -L/usr/local/lib -lboost_python36 -o account.so
Here is the basic cmake
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Using: make VERBOSE=1 the output commands are:
c++ -DBOOST_ALL_NO_LIB -Daccount_EXPORTS -I/usr/include/python3.6m -isystem /usr/local/include -fPIC -o CMakeFiles/account.dir/account.cpp.o -c /src/boost_python_example/account.cpp
c++ -fPIC -shared -Wl,-soname,account.so -o account.so CMakeFiles/account.dir/account.cpp.o /usr/local/lib/libboost_python36.a
So the cmake is not getting the same paths and flags, I am learning cmake so Im trying to understand this problem. Clearly the problem is not on actual libs but telling cmake where to find the proper ones.
The solution was quite simple. Comparing both commands what was missing on the cmake command was: --export-dynamic
So I solved using option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON) interesting enough the comment is needed.
Working solution:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Thanks everyone for the comments they lead me to a solution
I have an issue with linking FreeType with my project. I have my libs in a separate folder, {PROJECT}/lib, where libfreetype.a is located. My compiler is MinGW-64 4.8.3 x86_64-posix-seh-rev2, and I built libfreetype.a from source using this compiler. My cmake file looks like the following:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Build/Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(FREETYPE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include/)
set(FREETYPE_LIBRARY ${PROJECT_SOURCE_DIR}/lib/libfreetype.a)
find_package(Freetype REQUIRED)
set(SOURCE_FILES main.cpp ...)
add_executable(Wahoo ${SOURCE_FILES})
target_link_libraries(Test freetype)
Running this will cause the linker to fail with undefined references to '__imp_FT_Init_FreeType'. Removing the the libfreeType.a causes CMAKE say that it cannot find -lfreeType (as expected), but including libfreetype.a will cause the linker errors.
I have to change my answer, because I tried it on my own and it was bullshit.
I think, you have to change only your last line into
target_link_libraries(Test ${FREETYPE_LIBRARY})
I am trying to make a simple cmake file to generate a makefile for my project.
Now, i have to first genereate some .o files and then compile the main script.
I use some std 11 functions so i need to define that flag.
My CMakeList.txt file looks something like this:
cmake_minimum_required(VERSION 2.8)
project(P)
#C++ compiler
set(CMAKE_CXX_COMPILER_INIT g++)
#Compiler flags config
set(CMAKE_CXX_FLAGS "-W")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "-I../..")
set(CMAKE_CXX_FLAGS "-lpthread")
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_CXX_FLAGS "-Iinclude/")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-lz")
set(CMAKE_CXX_FLAGS "-lrt")
set(CMAKE_CXX_STANDARD 11)
set_source_files_properties(a.c PROPERTIES LANGUAGE CXX)
add_library(A OBJECT a.h a.c)
set_source_files_properties(b.cpp PROPERTIES LANGUAGE CXX)
add_library(B OBJECT b.h b.cpp)
add_executable(P p.cpp $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)
Now, what happens is that when i run the makefile, the compiler gives me an error with b.cpp.It says that one of the functions is not defined and i know that that error is because is not compiling with standard 11.
How can i tell cmake to add std=c++11 flag when compiling object files?
I know what was wrong. Apparently you can't add the flags in different lines. I should've done something like:
set(CMAKE_CXX_FLAGS "-W -Wall -I../.. -lpthread -g -Iinclude -std=c++11 -lz -lrt")
I'm trying to build my project using curl, but I have this result :
undefined reference to `_imp__curl_easy_init'
This is my CMakeLists :
cmake_minimum_required(VERSION 2.8)
project(score)
set(SOURCE_FILES main.cpp)
add_executable(score ${SOURCE_FILES})
add_library(libcurl STATIC IMPORTED)
set_property(TARGET libcurl PROPERTY IMPORTED_LOCATION "c:/MinGW/lib")
SET(GCC_COVERAGE_LINK_FLAGS "-lcurl")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
Moreover, I put my file libcurl.a, etc... in the correct directory "c:/MinGW/lib".
Could you help me ?
You probably need to compile the source files that call the curl functions with the CURL_STATICLIB macro defined.
Do you have access to the curl-config utility? It's there when you build curl from source. Run it with the --cflags option to get the compiler flags required and the --libs option to get linker requirements.
For example, in my mingw environment, the cflags reported are -DCURL_STATICLIB -I/mingw/local/include and the lib flags reported are -L/mingw/local/lib -lcurl -lssl -lcrypto -lgdi32 -lwldap32 -lz -lws2_32.