CMake Linker Flags - c++

I have a C++ code divided into 2 files image_desc_try.cpp, common.cpp and common.h. This code involves usage of Nvidia CUDA and NVidia NVInfer.
I could compile the entire code using the following with success.
g++ -std=c++11 image_desc_try.cpp common.cpp -I /usr/local/cuda/include -L /usr/local/cuda/lib64 -lcudart -lnvinfer -lnvparsers
Now, I am trying to do the same thing with CMake,
include_directories( "/usr/local/cuda/include" )
add_executable( desc_server
common.cpp
image_desc_try.cpp
)
target_link_libraries( desc_server
${catkin_LIBRARIES}
/usr/lib/aarch64-linux-gnu/libnvinfer.so
/usr/lib/aarch64-linux-gnu/libnvparsers.so
/usr/local/cuda-10.0/targets/aarch64-linux/lib/libcudart.so
)
This is giving me linking errors:
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getPluginRegistry'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getInferLibVersion'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getLogger'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `nvinfer1::utils::reshapeWeights(nvinfer1::Weights const&, int const*, int const*, void*, int)'
collect2: error: ld returned 1 exit status
tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/build.make:157: recipe for target '/home/dji/catkin_ws/devel/lib/tx2_whole_image_desc_server/desc_server' failed
make[2]: *** [/home/dji/catkin_ws/devel/lib/tx2_whole_image_desc_server/desc_server] Error 1
CMakeFiles/Makefile2:1578: recipe for target 'tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/all' failed
make[1]: *** [tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
How to fix this?

Related

Strange linking error while compiling mlpack

mlpack was perfectly compiling on my systems a few days back. I don't know what I did that's causing the following strange linking error:
[ 37%] Linking CXX shared library ../../lib/libmlpack.so
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib/libiberty.a(cplus-dem.o): warning: relocation against `libiberty_demanglers' in read-only section `.text'
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib/libiberty.a(cp-demangle.o): relocation R_X86_64_PC32 against symbol `cplus_demangle_builtin_types' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make[2]: *** [src/mlpack/CMakeFiles/mlpack.dir/build.make:1040: lib/libmlpack.so.3.4] Error 1
make[1]: *** [CMakeFiles/Makefile2:2106: src/mlpack/CMakeFiles/mlpack.dir/all] Error 2
make: *** [Makefile:146: all] Error 2
I tried adding the -fPIC flag to the cmake file but it still gives the same error

undefined reference to `cv::imwrite(...)'

I want to compile doppia, which has OpenCV as a dependency. Currently I am using OpenCV 2.4.9.1, which is the default on Ubuntu 16.04. Everything works fine besides the references to the function imwrite.
...
[100%] Linking CXX executable objects_detection
undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/objects_detection.dir/build.make:103: recipe for target 'objects_detection' failed
make[2]: *** [objects_detection] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/objects_detection.dir/all' failed
make[1]: *** [CMakeFiles/objects_detection.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
The file that throws the error is IntegralChannelsDetector.cpp.
In it, both opencv2/imgproc/imgproc.hpp and opencv2/highgui/highgui.hpp are included.
Running pkg-config opencv --cflags --libs also returns that all modules are linked correctly.
Researching about my error I learnt that if I was using OpenCV 3.x I'd have to include the img_codecs module, but that is not the case for OpenCV 2.x. I fear I'm stuck on 2.x for compatibility reasons.
Is there something left I could try to get the linking process completed?

Normaliz undefined reference to `boost::system::system_category()'

I'm trying to install the library Normaliz having dependencies like CocoALib, SCIP. I've made its dependencies according to their guidances. However, I get the error while make of Normaliz on both Ubuntu and Mac OSX,
[ 81%] Linking CXX executable normaliz
libnormaliz/libnormaliz.so.3.4.0: undefined reference to `boost::system::system_category()'
libnormaliz/libnormaliz.so.3.4.0: undefined reference to `boost::system::generic_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/normaliz.dir/build.make:99: recipe for target 'normaliz' failed
make[2]: *** [normaliz] Error 1
CMakeFiles/Makefile2:99: recipe for target 'CMakeFiles/normaliz.dir/all' failed
make[1]: *** [CMakeFiles/normaliz.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
I really fed up with it since two weeks! How can it be overcome?
Even if it is boost linkage problem, how can it be linked? There is no option for it. That kinda headaches cause apathy to open-source at least for young programmers like me.

How to fix undefined reference to `__imp_WSACleanup' (Boost.Asio) in CLion

My C++ project includes Boost and WebSocket++ libs. I read other topics I need to add -lws2_32 to gcc command, but I've CLion and I don't know how to fix it.
Console:
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::winsock_init_base::startup(boost::asio::detail::winsock_init_base::data&, unsigned char, unsigned char)':
C:/MinGW/include/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `__imp_WSAStartup'
CMakeFiles\WsServer.dir/objects.a(main.cpp.obj): In function `boost::asio::detail::winsock_init_base::cleanup(boost::asio::detail::winsock_init_base::data&)':
C:/MinGW/include/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\WsServer.dir\build.make:99: WsServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/WsServer.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/WsServer.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: WsServer] Error 2
What I should do?
You can pass ws2_32 via target_link_libraries in your CMakeLists.txt, e.g.:
if(MINGW)
target_link_libraries(YourTargetName ws2_32)
endif()

Error with Poco

I want to use Poco libraries in my C++ project.
But I get an error on compilation.
I'm developing on Ubuntu OS, CLion 1.2.4 IDE
Here it is my source code:
#include "Poco/Net/ServerSocket.h"
#include <iostream>
int main(int arc, char** argv){
std::cout << "Hello world!";
return 0;
}
And this is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.3)
project(rcp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
include_directories("libraries")
add_executable(rcp ${SOURCE_FILES})
target_link_libraries(rcp pthread PocoNet PocoUtil PocoFoundation)
And this is the output when I do the compilation:
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
main.cpp:(.text._ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv]+0x11): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int, unsigned int)'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0x8): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD0Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0xc): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0x8): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD0Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0xc): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o:(.rodata._ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
CMakeFiles/rcp.dir/main.cpp.o:(.rodata._ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
collect2: error: ld returned 1 exit status
CMakeFiles/rcp.dir/build.make:94: recipe for target '/home/john/projects/rightChoiceProperty/bin/rcp' failed
make[3]: *** [/home/john/projects/rightChoiceProperty/bin/rcp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rcp.dir/all' failed
make[2]: *** [CMakeFiles/rcp.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rcp.dir/rule' failed
make[1]: *** [CMakeFiles/rcp.dir/rule] Error 2
Makefile:118: recipe for target 'rcp' failed
make: *** [rcp] Error 2
Where is my problem I have search all of today but I couldn't find anything please help me to resolve this problem.
Thank you every body
I resolved the problem, as " M.M " guide in above comment:
The output suggests you didn't actually link against Poco::Net.
Another possibility might be if you're using Poco headers from a
different version of Poco than the compiled library you are linking
against
I removed changed the include to include right library.
At first this was my include line:
#include "Poco/Net/ServerSocket.h"
Changed to
#include <Poco/Net/ServerSocket.h>
Everything is working fine now.
Thank you M.M