How to fix linking issue in cmake for open ni2 - build

I have a code which uses the header file OpenNI.h and I am using CMake. Every time I run make I get error:
[ 33%] Linking CXX executable oniproject
CMakeFiles/oniproject.dir/main.cpp.o: In function `openni::VideoStream::start()':
main.cpp:(.text._ZN6openni11VideoStream5startEv[_ZN6openni11VideoStream5startEv]+0x31):>undefined reference to `oniStreamStart'
collect2: error: ld returned 1 exit status
And my CMakeLists.txt looks like
find_library(OPENNI2_LIBRARY
NAMES OpenNI
PATHS /usr/lib64
)
find_path (OPENNI2_INCLUDES OpenNI.h
/usr/local/src/OpenNI2/Include
)
include_directories(${OPENNI2_INCLUDES})
# practice
add_executable(oniproject
main.cpp
Viewer.cpp
Viewer.h
)
target_link_libraries(oniproject ${OPENNI2_LIBRARY})
target_link_libraries(oniproject ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} )
include_directories (${LIBUSB_INCLUDE_DIRS})
link_directories (${LIBUSB_LIBRARY_DIRS})
Can someone please tell me how to fix this?

Related

cannot find the .so library when use link_directories() and target_link_libraries() in cmake

Ubuntu shows: /usr/bin/ld: cannot find -llibswmm5, ,when i use the CMakeLists.txt below. To figure out what CMake has done,i use make VERBOSE=1, it showd the address problem, even after i tried export LD_LIBRARY_PATH='pwd'
[ 50%] Linking CXX executable Tutorial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Tutorial.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Tutorial.dir/tutorial.cxx.o -o Tutorial -llibswmm5
/usr/bin/ld: cannot find -llibswmm5
collect2: error: ld returned 1 exit status
i tried the two versions below (according to modern CMake, find_librarires() is encouraged to use), i thought they should work, but none of them work:(. I am a starter for CMake. If anyone could give some comments on this small problem, it would be much appreciate.
cmake_minimum_required(VERSION 3.10)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)
link_directories(${PROJECT_BINARY_DIR})
target_link_libraries(Tutorial libswmm5)
cmake_minimum_required(VERSION 3.10)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)
find_library(SWMM5_LIBRARY
NAMES
libswmm5
HINTS
${PROJECT_BINARY_DIR}/prebuilt/
)
if ( NOT SWMM5_LIBRARY )
message( WARNING "SWMM library NOT FOUND")
else()
message( STATUS "SWMM library : ${SWMM5_LIBRARY}")
endif(SWMM5_LIBRARY)
target_link_libraries(Tutorial ${SWMM5_LIBRARY} )

How to make CMake or OpenCV link statically [cross compilation Linux -> Windows]

I want to build a statically, because I need to distribute the application on multiple platforms (Linux and Windows). OpenCV has been built and installed on a linux machine statically.
Here you can see the relevant parts that are included in my CMakeLists.txt that were supposed to make OpenCV link statically
#windows
message("Compiling for windows")
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(BUILD_SHARED_LIBS OFF) #this has no effect
set(CMAKE_LINK_LIBRARY_SUFFIX ".a") #this has no effect
#openCV
set(OpenCV_STATIC ON) #this has no effect
find_package(OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS} )
During linking I get this error, as it apparently is a library required by opencv:
/usr/lib/x86_64-linux-gnu/libpng.so: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
As you can see libpng.so is being used. Even though in that Folder a libpng.a file is also available.
This only happens with mingw, but using the "-static" flag inside
target_link_libraries( Tracking ${OpenCV_LIBS} "-static")
results in
/usr/bin/x86_64-w64-mingw32-ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpng.so'
collect2: error: ld returned 1 exit status
So It still uses the .so file instead of .a That is why I suspect I need to change the OpenCV configuration
I have spent hours on this, so help is appreciated.
This should force to link libpng statically:
set_target_properties(libpng PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(libpng PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

CMake error: linker command failed with exit code 1 and cpp.o files

I am using CMake to compile a small project.
Here is what I have written in CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
set (CMAKE_CXX_STANDARD 11)
project(DAF)
find_package(OpenCV REQUIRED)
include_directories(include)
include_directories(${OpenCV_INCLUDE_DIRS} )
file(GLOB Src_Sources "src/*.cpp")
file(GLOB Test_Sources "test/*.cpp")
add_executable(executable ${Src_Sources} ${Test_Sources})
target_link_libraries( executable include ${OpenCV_LIBS} )
I have two directories src and test. The src directory contains only the files that hold the functions, whereas the test directory contains the main file.
Once I use the cmake command and then the make command I get this error:
Scanning dependencies of target executation
[ 25%] Building CXX object CMakeFiles/executation.dir/src/image.cpp.o
[ 50%] Building CXX object CMakeFiles/executation.dir/test/starter_1.cpp.o
[ 75%] Linking CXX executable executation
ld: library not found for -linclude
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [executation] Error 1
make[1]: *** [CMakeFiles/executation.dir/all] Error 2
make: *** [all] Error 2
So what I find strange is that it creates .cpp.o files. Is that normal?
And then how do I fix the error library not found for -linclude?
Yes, it is normal for CMake to take the name of the source file, and append a .o when compiling the respective object files. For example, the source file image.cpp will be compiled into image.cpp.o. The Makefiles generated by CMake will contain a unique target <someSourceFileName>.o for each source file, where <someSourceFileName> can be a .cc, .cpp, .c, etc.
The error:
ld: library not found for -linclude
indicates that you have tried to link a library called include to the executable. This is likely not what you intend. It appears include is actually your include directory, and you have already specified the include directories in your CMake with this line:
include_directories(include)
To remove the error, just take out the include from the target_link_libraries() command, like so:
target_link_libraries( executable ${OpenCV_LIBS} )

Building library with cmake

I apologize for bothering you all, but I have a little compilation problem with cmake.
I have a CMakeLists.txt file I'm using to build a test executable, and a shared library. They both have dependency to another library (SFML).
I'm using cmake on window with MinGW.
I know the name of the lib I'm building is kinda confusing with the sfml one, but it's supposed to be a SFML wrapper, so, I didn't find a better name!
Here the CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(projectName)
set(EXECUTABLE_NAME testSFML)
set(LIBRARY_NAME SFMLwindow)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include /
${CMAKE_CURRENT_SOURCE_DIR}/../../include
)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/)
file(
GLOB_RECURSE
SRC_FILES
src/*
)
file(
GLOB_RECURSE
INCLUDE_FILES
include/*
)
add_executable(
${EXECUTABLE_NAME}
main.cpp
${SRC_FILES}
${INCLUDE_FILES}
)
target_link_libraries(
${EXECUTABLE_NAME}
sfml-main
sfml-system
sfml-window
)
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
)
And what I get in the terminal :
"C:\MinGW\bin\mingw32-make.exe"
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/iksemel/docs/WorkBench/programming/projets/TestSFML/cmake
Linking CXX shared library libSFMLwindow.dll
Creating library file: libSFMLwindow.dll.a
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x59):undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0xda): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x163): undefined reference to `_imp___ZN2sf6Window5closeEv'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1bd): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1d8): undefined reference to `_imp___ZN2sf6Window7displayEv'
collect2: ld a retourné 1 code d'état d'exécution
mingw32-make.exe[2]: *** [libSFMLwindow.dll] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/SFMLwindow.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
If anybody have a clue on what's happening, I'd be very gratefull!
At a guess, your SFMLwindow library needs linked to some or all of sfml-main, sfml-system, sfml-window.
You could try changing the end of your CMakeLists.txt to:
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
${INCLUDE_FILES}
)
add_executable(
${EXECUTABLE_NAME}
main.cpp
)
target_link_libraries(
${LIBRARY_NAME}
sfml-main
sfml-system
sfml-window
)
target_link_libraries(
${EXECUTABLE_NAME}
${LIBRARY_NAME}
)
As an aside, file(GLOB_RECURSE... is generally frowned upon as a way to gather a list of sources. From the docs for file:
We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
Also, find_library should be preferred to link_directories in this case. From the docs for link_directories:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.

Cmake compile with Frameworks on Mac OSX and treat .cpp files like .m/.mm

I'm looking for a tip to get the following to work, here is my CMakeLists.txt
# cmake_minimum_required(2.8.2)
project(boilerplate)
# base files
set(src_files
src/greet.h
src/main.cpp
)
# if on OSX, these files are needed
if(APPLE)
SET(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -w")
set(src_files
${src_files}
src/mac/greet.mm
src/mac/greeting.h
src/mac/greeting.m
)
endif()
# if on windows, these files are needed
if(WIN32)
set(src_files
${src_files}
src/win/greet.cpp
)
endif()
add_executable(greeting
${src_files}
)
I require that on OSX the .cpp files are treated like .mm files (but on Windows, not) and that I can load the core foundation, etc frameworks... I'm a complete cmake newbie, so I can't even begin to know where to start, but I hope I'm somehow in the right direction, current output is:
$ cmake CMakeLists.txt && make
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/leehambley/Projects/watched.it-client
Scanning dependencies of target greeting
[ 33%] Building CXX object CMakeFiles/greeting.dir/src/mac/greet.o
Linking CXX executable greeting
Undefined symbols:
"greet()", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [greeting] Error 1
make[1]: *** [CMakeFiles/greeting.dir/all] Error 2
make: *** [all] Error 2
1
This turned out to be rather easy once I understood what was supposed to be happening under the hood:
set(CMAKE_CXX_FLAGS "-x objective-c++")
Which tells gcc that you want to set the language property (-x language, in man gcc) to Objective-C++.
You can also do this for individual files with:
set_source_files_properties(${SOURCE_FILES} PROPERTIES
COMPILE_FLAGS "-x objective-c++")
I've had mixed success with both, probably highlighting some of the things I don't fully understand about CMake.