linker problems with qt4 and CMake - c++

I'm trying to do a Qt project (Qt 4.8.6) with CMake, but i have some issues with the linker and i'm a bit lost.
Here is what i get when i do a "make" on my project:
chasselas:build tfaux$ make VERBOSE=1
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -H/Users/tfaux/Documents/workspace/tempQt4 -B/Users/tfaux/Documents/workspace/tempQt4/build --check-build-system CMakeFiles/Makefile.cmake 0
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_progress_start /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/addressbook.dir/build.make CMakeFiles/addressbook.dir/depend
cd /Users/tfaux/Documents/workspace/tempQt4/build && "/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_depends "Unix Makefiles" /Users/tfaux/Documents/workspace/tempQt4 /Users/tfaux/Documents/workspace/tempQt4 /Users/tfaux/Documents/workspace/tempQt4/build /Users/tfaux/Documents/workspace/tempQt4/build /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles/addressbook.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/addressbook.dir/build.make CMakeFiles/addressbook.dir/build
Linking CXX executable addressbook
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/addressbook.dir/link.txt --verbose=1
/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/addressbook.dir/main.cpp.o CMakeFiles/addressbook.dir/addressbook.cpp.o -o addressbook -F/usr/local/Cellar/qt/4.8.6/lib -framework QtOpenGL -framework QtGui -framework QtCore
Undefined symbols for architecture x86_64:
"AddressBook::staticMetaObject", referenced from:
AddressBook::tr(char const*, char const*) in addressbook.cpp.o
"vtable for AddressBook", referenced from:
AddressBook::AddressBook(QWidget*) in addressbook.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [addressbook] Error 1
make[1]: *** [CMakeFiles/addressbook.dir/all] Error 2
make: *** [all] Error 2
Here is the addressbook.h:
http://qt-project.org/doc/qt-4.8/tutorials-addressbook-fr-part1-addressbook-h.html
Here is the adressebook.cpp:
http://qt-project.org/doc/qt-4.8/tutorials-addressbook-fr-part1-addressbook-cpp.html
Here is the CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
LIST(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/qt/4.8.6")
#Test Cmake file for Qt applications
SET(PROJECT_NAME addressbook)
PROJECT(${PROJECT_NAME})
Find_PACKAGE(Qt4 REQUIRED)
SET(QT_USE_MAIN true)
#Setting the sources of the project
SET(PROJECT_SOURCES
main.cpp
addressbook.cpp
)
SET(PROJECTS_HEADERS
main.h
addressbook.h
)
#Traitement du moc Qt
QT4_WRAP_CPP(PROJECT_HEADERS_MOC ${PROJECT_HEADERS})
#inclusions
INCLUDE_DIRECTORIES(${TULIP_INCLUDE_DIR} ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
#creation de l'executable et liaison des librairies
ADD_EXECUTABLE(addressbook ${PROJECT_SOURCES} ${PROJECT_HEADERS_MOC})
TARGET_LINK_LIBRARIES(addressbook ${QT_LIBRARIES} ${QT_NETWORK_LIBRARY})
INSTALL(TARGETS addressbook DESTINATION bin)
Any help is welcome, thank you!

Use lower-case commands in your cmake code. It is far more readable.
You have a typo. You set the PROJECTS_HEADERS variable but you pass PROJECT_HEADERS to qt4_wrap_cpp. Prefer to use an editor which makes that obvious by highlighting identical strings when selected. Fixing the typo fixes your bug.
Use CMAKE_AUTOMOC instead of qt4_wrap_cpp. http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html It is available since CMake 2.8.6.

Related

C++ VCPKG: ld: library not found for -lprotobuf

I'm working on a C++ Project with CMake and VCPKG for Dependencies.
After I installed by dependencies: OpenCV, gRPC, Protobuf and OpenSSL,
I get a bunch of warnings and a linker error when I start my "main" Application.
Configuration is executed as
cmake -S . -B build-files/ "-DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake"
This is output of CMake: Pastebin.
Build is executed as
cmake --build build-files/ --target myproject.Core -v
This is its output:
/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 myproject.Core
/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_progress_start /Users/oliverkarger/Desktop/myproject/build-files/CMakeFiles 8
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 src/Core/CMakeFiles/myproject.Core.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/DependInfo.cmake --color=
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.grpc.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/build
make[3]: Nothing to be done for `libraries/Protos/CMakeFiles/myproject.Protos.dir/build'.
[ 62%] Built target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/src/Core /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/src/Core /Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/DependInfo.cmake --color=
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/Core.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Core
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/build
[ 75%] Linking CXX executable /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core
cd /Users/oliverkarger/Desktop/myproject/build-files/src/Core && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_link_script CMakeFiles/myproject.Core.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o CMakeFiles/myproject.Core.dir/Core.cc.o -o /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core -L/Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib -L/Users/oliverkarger/Desktop/myproject/build/archive -lgRPC /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libre2.a /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libcares.a /Users/oliverkarger/Desktop/myproject/build/archive/libmyproject.Protos.a -lresolv -lgRPC -lprotobuf
ld: library not found for -lprotobuf
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core] Error 1
make[2]: *** [src/Core/CMakeFiles/myproject.Core.dir/all] Error 2
make[1]: *** [src/Core/CMakeFiles/myproject.Core.dir/rule] Error 2
make: *** [myproject.Core] Error 2
Project: myproject.Protos is a self-built library dependency of myproject.core
CMake Files:
CMakeLists.txt
# Project Name
project("myproject")
# Options
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED 17)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ../../../build/archive)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../../build/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ../../../build/runtime)
# Application Metadata
set (CMAKE_PROJECT_VERSION_MAJOR 1)
set (CMAKE_PROJECT_VERSION_MINOR 0)
set (CMAKE_PROJECT_VERSION_PATCH 0)
set (CMAKE_PROJECT_VERSION_TWEAK 0)
# CMake Required Version
cmake_minimum_required(VERSION 3.24)
# Add VCPKG Toolchain
include (./vcpkg/scripts/buildsystems/vcpkg.cmake)
set (VCPKG_TARGET_TRIPLET x64-osx)
# Packages
find_package(gRPC CONFIG REQUIRED)
find_package(protobuf CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)
# Third Party Includes
include_directories(./vcpkg/installed/x64-osx/include)
include_directories("include")
include_directories(${OpenCV_INCLUDE_DIRS})
# Doxygen
find_package(Doxygen)
if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} #ONLY)
message("Doxygen build started")
# note the option ALL which allows to build the docs together with the application
add_custom_target( docs ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM )
else (DOXYGEN_FOUND)
message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)
# Google Test
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Directories
add_subdirectory(src/Core)
add_subdirectory(src/CoreGUI)
add_subdirectory(src/CameraServer)
# add_subdirectory(src/TestRunner)
add_subdirectory(libraries/Math)
add_subdirectory(libraries/ImageProcessing)
add_subdirectory(libraries/DataStructures)
add_subdirectory(libraries/Protos)
add_subdirectory(libraries/CommonCC)
src/myproject.Core/CMakeLists.txt
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.cc *.h)
set(SOURCES ${SOURCES})
add_executable(myproject.Core ${SOURCES})
target_link_libraries(myproject.Core gRPC)
target_link_libraries(myproject.Core re2::re2 c-ares::cares)
target_link_libraries(myproject.Core myproject.Protos)
libraries/myproject.Protos/CMakeLists.txt
file(GLOB_RECURSE PROTOS LIST_DIRECTORIES true *.proto)
set(PROTOS ${PROTOS})
add_library(myproject.Protos ${PROTOS})
target_link_libraries(myproject.Protos gRPC)
target_link_libraries(myproject.Protos protobuf)
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)
protobuf_generate(TARGET myproject.Protos LANGUAGE cpp)
protobuf_generate(TARGET myproject.Protos LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
As far as I understand the issue is that cmake can't find the library file for protobuf. But I checked with vcpkg (is a submodule of my project) that the file and path both exist.
I also tried passing the path to the .a-file directly by using
target_link_library(myproject.(Core|Protos) vcpkg/installed/x64-osx/lib/libprotobuf.a)
but that didn't resolve my issues.
How to link libprotobuf properly?

Simple OpenMP-enabled code does not compile: "clang: error: linker command failed with exit code 1"

I tried to test OpenMP on MacOS with the Apple Clang compiler that comes with Xcode.
Code:
#include <iostream>
#include <cmath>
#include </usr/local/opt/libomp/include/omp.h>
int main() {
std::cout << "Hello, World!" << std::endl;
#pragma omp parallel
{
printf("Hello World! \n");
}
return 0;
}
and this is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
include (FindOpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
message (VERBOSE "OpenML library found")
else()
message (FATAL_ERROR "OpenML library not found (required for multithreading)")
endif ()
add_executable(untitled main.cpp)
This is the compiler's error:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
The OpenMP is installed using hombrew libomp I also tried adding environment variable LDFLAGS=-L/usr/local/opt/libomp/lib/ and it does not affect the result
UPDATE: COMPILATION WITH VERBOSE=1:
...
[100%] Linking CXX executable untitled
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/untitled.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -Xclang -fopenmp -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/libomp/lib/ CMakeFiles/untitled.dir/main.cpp.o -o untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
By using set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") and using set in other places, you are replacing the flags that CMAKE puts. It is better to append in this case rather than replace. You can use add_compile_options(-Wall) here.
As mentioned by #Tsyvarev, it is perhaps better to use imported library. Here are a few other ways you can use target_link_libraries.
For your particular case, it is best to replace
find_package(OpenMP REQUIRED)
where you currently have include (FindOpenMP). Further, remove the if condition as well. Finally, add the following to the end of your CMakeLists.txt file.
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Here is a good explanation of the difference between PRIVATE and PUBLIC.
Your final CMakeLists.txt file should look like:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenMP REQUIRED)
add_executable(
untitled
main.cpp
)
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Thanks to Tsyvarev, for helping me with CMake a few months ago. Further, thanks to the answers that Tsyvarev points toward, which my answer is based on (along with my own testing).

CMake - Link library included into the project

I'm trying to include the GMP library into my project.
This is my folder tree:
As you can see, the gmp folder (that contains include and lib) is into the include folder.
This is my app/CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
add_executable(
${PROJECT_NAME}
../include/KMC/kmc_api/kmc_file.cpp
../include/KMC/kmc_api/kmer_api.cpp
../include/KMC/kmc_api/mmer.cpp
main.cpp
)
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
)
target_link_directories(${PROJECT_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include/gmp/lib
)
target_link_libraries(${PROJECT_NAME} PUBLIC
gmp
gmpxx
)
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_SOURCE_DIR}/include/gmp/include
)
target_compile_definitions(${PROJECT_NAME} PRIVATE DEBUG=1)
and this, is the CMakeLists.txt in the project root:
cmake_minimum_required(VERSION 3.18)
set(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
project(MyProj)
enable_language(CXX)
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 1)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wpedantic -pedantic-errors -fopenmp")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
include(CMakeToolsHelpers OPTIONAL)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories(include)
include_directories(include/KMC)
add_subdirectory(app)
After, cd build && cmake .. I run make -j12 and than, -the compilation fail:
[100%] Linking CXX executable DynaDBG
cd /Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build/app && /usr/local/Cellar/cmake/3.18.4/bin/cmake -E cmake_link_script CMakeFiles/DynaDBG.dir/link.txt --verbose=1
/usr/local/bin/g++ -Wall -Wextra -Wpedantic -pedantic-errors -fopenmp -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/kmc_file.cpp.o CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/kmer_api.cpp.o CMakeFiles/DynaDBG.dir/__/include/KMC/kmc_api/mmer.cpp.o CMakeFiles/DynaDBG.dir/main.cpp.o -o DynaDBG -L/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/include/gmp/lib -Wl,-rpath,/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/include/gmp/lib -lgmp -lgmpxx
Undefined symbols for architecture x86_64:
"__ZlsRSoPK12__mpz_struct", referenced from:
__ZlsIA1_12__mpz_structS1_ERSoS2_RK10__gmp_exprIT_T0_E in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [app/CMakeFiles/DynaDBG.dir/build.make:151: app/DynaDBG] Error 1
make[2]: Leaving directory '/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build'
make[1]: *** [CMakeFiles/Makefile2:116: app/CMakeFiles/DynaDBG.dir/all] Error 2
make[1]: Leaving directory '/Users/th3g3ntl3man/Desktop/Repository/DynaDBG/build'
make: *** [Makefile:106: all] Error 2
UPDATE:
Compiling the example I have this error:

Cross compiling for arm android

i want to build c++ library for my android device, more specific i want to build taglib to use with my Qt project, i have a music app that uses taglib, i build it for windows and ubuntu and it works as expected, but now i want to cross compile taglib to use on my android phone, i searched hours to find the solution, i found some article about toolchain file and manual cmake option but none of them worked for me,
i tried to set CMAKE_SYSTEM_NAME to android and set other Cmake option like below:
set(CMAKE_SYSTEM_NAME Android)
message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} {CMAKE_SYSTEM_VERSION}")
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)
and when i check the generated .so file with file command it show my system info like below:
libmylib.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=4245460a39baea2cbd1f28c3a2fd8037b07fe995, with debug_info, not stripped
so can anyone give me some link or help to do this?
** update
my cmake command is like below:
cmake CMakeLists.txt -DCMAKE_SYSTEM_NAME=ANDROID -DCMAKE_TOOLCHAIN_FILE=/home/sub/Downloads/android-ndk-r21/build/cmake/android.toolchain.cmake
now i use new ndk and use it android.toolchain.cmake file but the output say it doesn't use it:
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
-- Build files have been written to: /home/sub/Documents/Projects/cpp/build-for-android
my new CMakeLists.txt is :
cmake_minimum_required(VERSION 3.17)
project(addlib CXX)
# set(CMAKE_SYSTEM_NAME Android)
# message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")
# set(CMAKE_SYSTEM_VERSION 21)
# set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
# set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
# set(CMAKE_ANDROID_STL_TYPE gnustl_static)
set(BUILD_SHARED_LIBS true)
add_library(mylib main.cpp)
target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
message(${CMAKE_CROSSCOMPILING})
install(TARGETS mylib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/)
make command output with verbose on is same as below:
/usr/local/bin/cmake -S/home/sub/Documents/Projects/cpp/build-for-android -B/home/sub/Documents/Projects/cpp/build-for-android --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/depend
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
cd /home/sub/Documents/Projects/cpp/build-for-android && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/mylib.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/build
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
[ 50%] Linking CXX shared library libmylib.so
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/mylib.dir/link.txt --verbose=1
/usr/bin/g++ -fPIC -g -shared -Wl,-soname,libmylib.so -o libmylib.so CMakeFiles/mylib.dir/main.cpp.o
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
[100%] Built target mylib
make[1]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles 0
thank you

error linking GMP library

I have the following CMakeList.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
with the following simple source file main.cpp
#include <iostream>
#include <gmp.h>
#include <cstdio>
using namespace std;
int main(){
mpz_t a,b;
mpz_init_set_str(a,"45",10);
mpz_init_set_str(b,"12",10);
mpz_add(a,a,b);
cout <<mpz_get_str(NULL,10,a)<<endl;
}
I have included a FindGMP.cmake file that i found online :
set(GMP_PREFIX "" CACHE PATH "path ")
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
PATHS ${GMP_PREFIX}/include /usr/include /usr/local/include )
find_library(GMP_LIBRARY NAMES gmp libgmp
PATHS ${GMP_PREFIX}/lib /usr/lib /usr/local/lib)
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
get_filename_component(GMP_LIBRARY_DIR ${GMP_LIBRARY} PATH)
set(GMP_FOUND TRUE)
endif()
if(GMP_FOUND)
if(NOT GMP_FIND_QUIETLY)
MESSAGE(STATUS "Found GMP: ${GMP_LIBRARY}")
endif()
elseif(GMP_FOUND)
if(GMP_FIND_REQUIRED)
message(FATAL_ERROR "Could not find GMP")
endif()
endif(
when I run cmake I have the following output:
moyle#localhost:~/Desktop/gmptri$ cmake .
-- Found GMP: /usr/local/lib/libgmp.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/moyle/Desktop/gmptri
Then I run make and i get the following errors :
(precise)moyle#localhost:~/Desktop/gmptri$ make VERBOSE=1
/usr/bin/cmake -H/home/moyle/Desktop/gmptri -B/home/moyle/Desktop/gmptri --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/moyle/Desktop/gmptri/CMakeFiles /home/moyle/Desktop/gmptri/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/depend
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
cd /home/moyle/Desktop/gmptri && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri/CMakeFiles/Turrial.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/build
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
Linking CXX executable Turrial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Turrial.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Turrial.dir/main.cpp.o -o Turrial -rdynamic
CMakeFiles/Turrial.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x30): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x47): undefined reference to `__gmpz_add'
collect2: ld returned 1 exit status
make[2]: *** [Turrial] Error 1
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make[1]: *** [CMakeFiles/Turrial.dir/all] Error 2
make[1]: Leaving directory `/home/moyle/Desktop/gmptri'
make: *** [all] Error 2
can anyone help me?
You forget to link libraries with your executable.
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
target_link_libraries(Turrial gmp libgmp)