I'm trying to build my C++ project which includes Pjsip library on Centos 7 using CMake and Qt IDE without success. I built Pjsip for Centos using steps defined in readme.txt :
./configure
make dep && make
After that configured CMakeLists.txt with this configuration:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_INSTALL_PREFIX /opt)
project(hello-pjsip)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DSERVER_REMOTE_LOGGING -ggdb -Wno-unused-local-typedefs")
# Set the compilers as the default GCC for Centos 7 doesn't support all C++11 features
set(CMAKE_CXX_COMPILER /opt/rh/devtoolset-7/root/bin/g++)
set(CMAKE_C_COMPILER /opt/rh/devtoolset-7/root/bin/gcc)
# PJ library
include_directories(~/pjproject/pjlib/include)
link_directories(~/pjproject/pjlib/lib)
include_directories(~/pjproject/pjlib-util/include)
link_directories(~/pjproject/pjlib-util/lib)
include_directories(~/pjproject/pjnath/include)
include_directories(~/pjproject/pjnath/src)
link_directories(~/pjproject/pjnath/lib)
# Source files
set (SOURCE_FILES
main.h
main.cpp
)
# Library files
set (LIBRARY_FILES
pj-x86_64-unknown-linux-gnu
pjnath-x86_64-unknown-linux-gnu
pjlib-util-x86_64-unknown-linux-gnu
)
add_executable(hello-pjsip ${SOURCE_FILES})
target_link_libraries(hello-pjsip ${LIBRARY_FILES})
After running build received these errors:
I tried to recompile pjsip library with different options but no success, same errors every time. Could someone help me?
PJ had bug, solved 5 months ago. This solved problem:
https://trac.pjsip.org/repos/changeset/5599
Related
I am developing a C++ library, including OpenCV, which will be used in a cross-platform Xamarin solution through a wrapper and the NuGet packaging system (see this guide). I configured a CMakeLists.txt file but I simply cannot get OpenCV to be correctly linked for both static (iOS) and dynamic (Android) libraries.
I tried to change the OpenCV_DIR variable, install and build OpenCV from sources and manually include the content of the OpenCV_INCLUDE_DIRS variable but nothing worked. I also noticed that the linking works when only using cv::Point. But the linking does not work when using cv::Mat, which I do not understand the reason.
The following is the CMakeLists.txt that I am using :
cmake_minimum_required (VERSION 3.2)
project (MyLib C CXX)
enable_testing()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
MESSAGE(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
# Source and headers files
set(SOURCES File1.cpp File2.cpp)
set(HEADERS File1.h File2.h)
# Library
if(BUILD_SHARED_LIBS)
add_library (MyLib SHARED ${SOURCES} ${HEADERS})
target_compile_definitions(MyLib PUBLIC IS_BUILDING_SHARED)
else()
add_library (MyLib STATIC ${SOURCES} ${HEADERS})
endif()
# Dependencies
set(OpenCV_DIR /usr/local/Cellar/opencv/4.5.0_1/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
message(STATUS "OpenCV_DIR = ${OpenCV_DIR}")
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MyLib ${OpenCV_LIBS})
The following shows the location of OpenCV's files that are used during the build process. Everything seems alright.
-- OpenCV_INCLUDE_DIRS = /usr/local/Cellar/opencv/4.5.0_1/include/opencv4
-- OpenCV_LIBS = opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_alphamat;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_sfm;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- OpenCV_DIR = /usr/local/Cellar/opencv/4.5.0_1/lib/cmake/opencv4
Android
The following is the commands that I am using to build the Android dynamic library (.so). I have installed the NDK and am building for each ABI (x86, x86_64, armeabi-v7a, arm64-v8a).
cmake ../.. -DCMAKE_TOOLCHAIN_FILE=/Users/$USER/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 -DANDROID_ABI=$abi_name -DBUILD_SHARED_LIBS=ON
cmake --build . --config Release
I directly get an error when building the library which is the following.
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_gapi.4.5.0.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_stitching.4.5.0.dylib: unknown file type
[...]
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_rapid.4.5.0.dylib: unknown file type
ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
iOS
The following is the commands that I am using to build the iOS static library (.a). I am using leetal's ​cmake toolchain file from this repository.
cmake ../.. -G Xcode -DCMAKE_TOOLCHAIN_FILE=../../ios.toolchain.cmake -DPLATFORM=OS64COMBINED -DBUILD_SHARED_LIBS=OFF
cmake --build . --config Release
The compilation of the static library seems to work because no error message is printed. However, when the library is used in the final Xamarin solution, the linked library cannot be found and the following error is shown.
Native linking failed, undefined symbol: cv::Mat::deallocate(). Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in. (MT5210)
Question
What am I missing in order to properly compile and link OpenCV into my C++ library ?
I am working on macOS Big Sur and uses the following tools versions:
cmake : 3.20.0-rc5
ndk : 23.0.7196353
apple clang : 12.0.0
I hope that the description of my problem is clear enough and I thank you in advance for any help.
We had same problems, including Xamarin's DllNotFoundException with message from last comment, which led me to this topic. What fixed the exception for us in the end was linking statically to OpenCV *.a libs instead of linking to the shared libopencv_java4.so file. So we now have a huge 30MB nativelib.so file for each android ABI in build output, instead of a pair of small nativelib.so and libopencv_java4.so per ABI. CMakeLists looks like this:
set( OpenCV_DIR "~/opencv/build/OpenCV-android-sdk/sdk/native/jni" )
find_package( OpenCV REQUIRED )
target_link_libraries( # Specifies the target library.
nativelib
${OpenCV_LIBS})
Another thing in our project is we use OpenCV optional modules and had to create a custom OpenCV build, which I guess ensures our native library and OpenCV are compiled against same NDK version. I suppose using the prebuilt OpenCV distribution and compiling against a different NDK version could lead to problems too otherwise.
I want to cross-compile Qt planets demo applicatioin on Linux x64 with CMake for ARM.
I installed arm cross compiler. I've built Qt libs on ARM board and copied them to x64 Linux box's /lib/arm-linux-gnueabihf.
For qmake I did this with only adding few lines to planets.pro file:
PLATFORM = arm-linux-gnueabihf
QMAKE_CXX = /usr/bin/$$PLATFORM-g++
QMAKE_LINK = /usr/bin/$$PLATFORM-g++
QMAKE_LIBS = -L/usr/$$PLATFORM/lib -L/lib/$$PLATFORM
It compiles successfully and runs perfectly on ARM board.
Now, I want to do this with CMake. So, I created the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(PLATFORM arm-linux-gnueabihf)
set(CMAKE_CXX_COMPILER /usr/bin/${PLATFORM}-g++)
link_directories(/usr/${PLATFORM}/lib /lib/${PLATFORM})
file(GLOB_RECURSE HEADERS "*.hpp")
file(GLOB SOURCES "*.cpp")
set(CMAKE_AUTOMOC ON)
find_package(Qt5Qml REQUIRED)
find_package(Qt5Quick REQUIRED)
add_executable(planets-qml ${SOURCES})
target_link_libraries(planets-qml
Qt5::Qml
Qt5::Quick
)
With this I have the following warning in Qt Creator's log:
Cannot generate a safe runtime search path for target planets-qml because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libGL.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/lib/arm-linux-gnueabihf
Then, I'm building it, and I got linker error:
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Quick.so.5.7.0:
file not recognized: File format not recognized
I assume this happens because linker tries to link x64 Qt libraries to ARM binary.
But why can't it find libs from /lib/arm-linux-gnueabihf ?
I found the command to the linker in the file linker.txt:
/usr/bin/arm-linux-gnueabihf-g++
CMakeFiles/planets-qml.dir/main.cpp.o
CMakeFiles/planets-qml.dir/planets-qml_automoc.cpp.o
-o planets-qml
-L/usr/arm-linux-gnueabihf/lib -L/lib/arm-linux-gnueabihf
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Quick.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Qml.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Network.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Gui.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Core.so.5.7.0
-Wl,-rpath,/usr/arm-linux-gnueabihf/lib:/lib/arm-linux-gnueabihf:/home/dev/Qt5.7.0/5.7/gcc_64/lib
So, the question is: How to exclude these /home/dev/Qt5... entries and not to pass them to the linker?
What CMake command should I use in CMakeLists.txt file?
Problem is that you are looking at the wrong QT directory.
You need to recompile Qt for ARM. Then set Qt5_DIR before find_package calls.
set(Qt5_DIR <path-to-Qt>/lib/cmake/Qt5/)
Your <path-to-Qt>/lib/cmake/Qt5/ should look like this:
$ ls <path-to-Qt>/lib/cmake/Qt5/
Qt5Config.cmake Qt5ConfigVersion.cmake
I put
set(CMAKE_CXX_COMPILER "/usr/bin/clang.exe")
Run/Clean, Run/Build.
I get link errors like:
undefined reference to `std::ios_base::Init::~Init()'
: undefined reference to `__gxx_personality_v0'
Presumably there are other variables to change. Tried adding -lstdc++ to CMAKE_CXX_FLAGS, but no different.
Is there a CLion way as opposed to a CMake way, for example?
Thanks.
Specifying a compiler with CMake is a bit delicate. Although the method you are using, setting CMAKE_CXX_COMPILER in CMakeLists.txt works, it is the least-recommended way in the CMake FAQ.
CLion supports method 2 of the CMake FAQ: using -D within the cmake invocation. Setting the variables in CMakeLists.txt has no effect.
On Mac, go to Preferences
On Linux/Windows, go to File | Settings
then Build, Execution, Deployment | CMake | CMake options and enter the text:
-D CMAKE_C_COMPILER=/path/to/c_compiler
-D CMAKE_CXX_COMPILER=/path/to/c++_compiler
See the CLion FAQ for details.
Note also that when you change compilers, you will have to invalidate the CLion cmake cache and restart, see my answer on How to clear CMake cache in Clion?.
EDIT
After I wrote this answer, CLion added support for multiple build directories, as pointed out by #rubenvb in the comments. This is another path to investigate.
In fact the latest version of Clions 2018.2 running on windows 10 environment work with LLVM clang version 6 /6.0.1 or even 7.0 along with GCC mingw x64 win32 specific variant.
linker default is set to GCC not visual studio
I guess it should work on cygwin too with the same setup as the following also tested to work on a number of popular c++ IDE also.
x64 or 32 specific GCC mingw version tested to work on Clions 2018.2
\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0 or mingw-w64\i686-8.1.0-win32-dwarf-rt_v6-rev0
CMake build setup as below
cmake_minimum_required(VERSION 3.10)
project(project_name )
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_COMPILER "c:/llvm/bin/clang.exe")
set(CMAKE_CXX_COMPILER "c:/llvm/bin/clang++.exe")
// target i686-pc-windows-gnu for 32bit
set(CL_COVERAGE_COMPILE_FLAGS "-v -target x86_64-pc-windows-gnu -Wall -Wextra -std=gnu++17")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CL_COVERAGE_COMPILE_FLAGS}" )
add_executable(project_name yourcpp.cpp)
I'm using the point cloud library and tried to downsample a point cloud using a voxel grid. It worked fine when I was using Xcode, where I manually added the required libraries, but not with cmake. The program compiles fine with cmake but if I run the program compiled by cmake I get the following error:
$ ./main
$ Bus error: 10
I figured out that the error only occurs when pcl/filters/voxel_grid.h is included. I used other classes of PCL in the program before and everything worked fine with cmake.
If also figured out that if pcl/filters/voxel_grid.h is included and I remove set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") from my cmake file the program will compile and run fine just like it did in Xcode(language dialect in Xcode is set to C++11). The problem is, I need c++11 features and also cmake since I need to be able to easily build the program on a different OS.
I'm on OS X 10.9.2 and using clang 3.4, boost 1.55.0, PCL 1.7.1 and CUDA 5.5.
It would be great if someone could help me.
Thanks for your help
Here is a minimal example:
//main.cpp
#include <pcl/filters/voxel_grid.h> // it works if this include is removed
int main(int argc, const char * argv[])
{
return 0;
}
The cmake file I use:
#cmake file
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(PCL 1.7 REQUIRED)
find_package(Boost 1.55 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS} ${BOOST_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${PCL_LIBRARIES} ${BOOST_LIBRARIES})
Solution/Error cause
I found the root cause of the problem. I compiled an example what causes the same problem manually on the console so that I could compare the different compiler flags/settings used by Xcode and cmake.
What I found out is, that Xcode adds some flags that cmake doesn't add. One of the flags/options Xcode adds is "-fvisibility-inlines-hidden". With that option set the example program compiles and runs fine, without the option set it compiles but generates a "Bus error: 10" at runtime.
It would be great if some could explain what that option does and why it's necessary when compiling. Is this just clang or OS X specific, so that on a different OS/compiler the option isn't needed?
Regards and thanks,
Ruediger
I've compiled a C++ static library by using CMake as my building tool and I want to link it to my iOS app.
I created a simple 'Empty' application in Xcode and linked my library called libengine.a to it.
I tried to compile my iOS project and the linker gave me this warning:
ignoring file /Users/.../build/engine/libengine.a,
file was built for archive which is not the architecture being linked (i386):
/Users/.../build/engine/libengine.a
As I understand it, I need to compile my library for ARM processors. The problem is I don't know how.
I think CMake really lacks good tutorials.
Anyways, my CMake scripts are attached below.
Any help would be greatly appreciated.
Thanks, Tal.
Here is my main CMake script:
cmake_minimum_required(VERSION 2.8)
project(movie-night)
if (DEFINED PLATFORM)
include(toolchains/ios.cmake)
endif()
add_definitions(-Wall)
set(DEBUG)
if (DEFINED DEBUG)
add_definitions(-g)
endif()
if (DEFINED RELEASE)
add_definitions(-O3)
endif()
add_subdirectory(engine)
add_subdirectory(ui)
add_subdirectory(test)
Here is my toolchains/ios.cmake file:
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm)
Just use this toolchain file: http://code.google.com/p/ios-cmake/ and use it as
cmake -DCMAKE_TOOLCHAIN_FILE=path_to_your_toolchain_file
Then, in CMakeLists.txt:
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch armv7")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch armv7")
By following cmake-toolchains documentation I did like below:
cmake -G Xcode -B build \
-DCMAKE_SYSTEM_NAME=iOS \
-DCMAKE_Swift_COMPILER_FORCED=true \
-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0
Note: That assignment CMAKE_OSX_DEPLOYMENT_TARGET=11.0 is not a mistake when targeting iOS.
I've been using an updated version of the iOS CMake toolchain for quite a while: https://github.com/leetal/ios-cmake
This creates an Xcode project which you can drag into an existing iOS project if necessary.
I wrote a blog post with more details: https://blog.tomtasche.at/2019/05/how-to-include-cmake-project-in-xcode.html
There is a second version of iOS.cmake located at:
https://ceres-solver.googlesource.com
Note: You may find that both versions of iOS.cmake will only build x86 versions of your project. CMake now sets CMAKE_OSX_SYSROOT to the (latest) Mac OS X SDK available on your system. You can fix this by modifying your copy of iOS.cmake to always set CMAKE_OSX_SYSROOT. You can do this by commenting out a couple of lines your copy of iOS.cmake:
# -- Under CMake 3.4.2, CMAKE_OSX_SYSROOT is automatically defined to point to the latest Mac OS X SDK. --
# -- So, the iOS SDK is never found. Grab the correct CMAKE_OS_SYSROOT and ignore any prior setting. --
# If user did not specify the SDK root to use, then query xcodebuild for it.
# if (NOT CMAKE_OSX_SYSROOT)
execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
message (STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
message (STATUS "be sure the previous line points to the correct SDK")
# endif ( )