Cmake Fails to Link DL Library - c++

Below is the CMakeLists I currently have however, I am having issues trying to get it to build and link with the dl library. The target_link_libraries statement did not seem to be effective in doing that (it got rid of the undefined reference errors). I then tried adding the linker flags however, it seems to error out with the following:
[build] g++-9: error: ;-Wl,--no-as-needed: No such file or directory
[build] make[2]: *** [CMakeFiles/FIFO_Queue_Testbench.dir/build.make:86: FIFO_Queue_Testbench] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:141: CMakeFiles/FIFO_Queue_Testbench.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2
I am pretty new to using meta build systems like CMake so if you could breakdown what was incorrect and why, I would appreciate the understanding. Thank you for any help in advance!
If it's of any importance I am on Ubuntu 20.04 using vscode.
cmake_minimum_required(VERSION "3.16")
project("My_CPP_Code" VERSION "0.0")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(MY_LINKER_FLAGS "-Wl,--no-as-needed -ldl")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}" "${MY_LINKER_FLAGS}")
set(XILINX_XSI_INCLUDE_DIR
"/tools/Xilinx/Vivado/2020.2/data/xsim/include"
)
set(XILINX_HEADER_FILES
"${XILINX_XSI_INCLUDE_DIR}/xsi.h"
)
include_directories("${XILINX_XSI_INCLUDE_DIR}")
add_subdirectory("Data_Structures")
add_subdirectory("Hardware_Models")
add_subdirectory("Xilinx")
set (XILINX
${XILINX_HEADER_FILES}
${XILINX_LOCAL_LIB}
)
add_executable("Main_App" "Applications/Main.cpp" ${XILINX})
add_executable("FIFO_Queue_Testbench" "Hardware_Models/FIFO_Queue_Testbench.cpp" ${XILINX})
target_link_libraries("Main_App"
INTERFACE "Data_Structures")
target_link_libraries("FIFO_Queue_Testbench"
${CMAKE_DL_LIBS}
"XILINX_LOCAL"
"Hardware_Models")

Related

Undefined Reference error with RGBD.hpp file from opencv

I've got a problem at the moment...
I've got a cmake file that includes the local computer files.
cmake_minimum_required(VERSION 3.10)
project(testproject)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -Wall -fPIC -Ofast -fopenmp")
link_directories(/usr/local/lib/cmake)
set(CMAKE_BUILD_TYPE "Debug")
find_package(OpenCV REQUIRED)
set(LIBRARY_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Libraries/include)
set(LIBRARY_USR_INCLUDE_DIR /usr/local/include)
add_executable(testproject
"base.cpp"
"Libraries/include/base64/base64.h"
"Libraries/include/base64/base64.cpp"
)
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${LIBRARY_USR_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS} ${LIBRARIES} ${LIBRARY_INCLUDE_DIR})
target_link_libraries(${CMAKE_PROJECT_NAME} "${OpenCV_LIBS}" stdc++fs)
The include file within CMAKE works because there are other files with opencv included and those are found..
Only the weird problem is that the rgbd.hpp (File is under /usr/local/include/) is found and also the function but when i'm making the the file with.
Terminal Process:
make -j7
ERROR RESULT:
[ 15%] Linking CXX executable count
CMakeFiles/count.dir/base.cpp.o: In function `main':
/home/testpc/Documents/Test/base.cpp:10: undefined reference to `cv::rgbd::DepthCleaner::DepthCleaner(int, int, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/count.dir/build.make:374: recipe for target 'count' failed
make[2]: *** [count] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/count.dir/all' failed
make[1]: *** [CMakeFiles/count.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
make: *** [all] Error 2
That this happens when all the other files are already have been compiled.
Problem class: Base.cpp
#include "opencv2/rgbd.hpp"
int main()
{
rgbd::DepthCleaner * depthc = new rgbd::DepthCleaner(CV_16U, 7, rgbd::DepthCleaner::DEPTH_CLEANER_NIL);
}
Possible Tried Solutions:
Still having the same result, When i remove the content within the main method and replace it with cout << "Test" << endl; It builds and runs as it suppose to be. Also works when I create a new raw Material blanko with standard Opencv core included.
It only does not build with the RGBD Include and instance creation.
(RGDB.hpp does not stand under /usr/local/inlclude) but under /usr/include. So i removed the local part but still responded as the same current issue. Any other possible solution?

Using OPCUA Library in a New Cmake Project

In this time, I want to use Freeopcua to create a library to use in other project.
I have install all the tools in debian.soft file and build with following command sudo sh build.sh, cmake ., make, sudo make install. Although, in directory /usr/local/include and /usr/local/lib, there are opc directory and libopc*.so respectively, when I create a new cmake project, there is an error. Hope someone can help me or give some suggest. Thanks everyone~~~
I have refer to Libraries in /usr/local/lib not found, but it doesn't work for me
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(OPCUAIndependent)
include_directories(/usr/local/include/)
link_directories(/usr/local/lib/)
add_executable(sourceCode sourceCode.cpp)
target_link_libraries(sourceCode opc)
Error
[ 50%] Linking CXX executable sourceCode
/usr/bin/ld: cannot find -lopc
collect2: error: ld returned 1 exit status
CMakeFiles/sourceCode.dir/build.make:96: recipe for target 'sourceCode' failed
make[2]: *** [sourceCode] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/sourceCode.dir/all' failed
make[1]: *** [CMakeFiles/sourceCode.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

Unable to compile example from OpenCL C++ Bindings Documentation (cl2.hpp)

I am trying to learn OpenCL2 with C++.
I'm using kubuntu 16.04.1 LTS and beignet from repository.
My laptop is a lenovo with a intel i5-5200U without nvidia or similar.
The command clinfo recognize the platform.
The first bug i found in the example is the variable output2 that isn't declared i have tried to comment it, but still get many link error...
The example is this http://github.khronos.org/OpenCL-CLHPP/index.html#example
This is a minimal example that reproduce a part of link error:
main.cpp:
// Defines the target OpenCL runtime version to build the header against.
// Defaults to 200, representing OpenCL 2.0.
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
int main()
{
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11
project(exampleopencl2)
add_executable(exampleopencl2 main.cpp)
install(TARGETS exampleopencl2 RUNTIME DESTINATION bin)
I use KDevelop and i get this output when i try to compile this code:
/home/deglans/ExampleOpenCL2/build> make -j2 mytests
Scanning dependencies of target mytests
[ 50%] Building CXX object CMakeFiles/mytests.dir/main2.cpp.o
[100%] Linking CXX executable mytests
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_device_id*>::release(_cl_device_id*)':
/usr/include/CL/cl2.hpp:1438: undefined reference to `clReleaseDevice'
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_context*>::release(_cl_context*)':
/usr/include/CL/cl2.hpp:1473: undefined reference to `clReleaseContext'
CMakeFiles/mytests.dir/main2.cpp.o: In function `cl::detail::ReferenceHandler<_cl_command_queue*>::release(_cl_command_queue*)':
/usr/include/CL/cl2.hpp:1482: undefined reference to `clReleaseCommandQueue'
collect2: error: ld returned 1 exit status
CMakeFiles/mytests.dir/build.make:94: recipe for target 'mytests' failed
make[3]: *** [mytests] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/mytests.dir/all' failed
make[2]: *** [CMakeFiles/mytests.dir/all] Error 2
CMakeFiles/Makefile2:116: recipe for target 'CMakeFiles/mytests.dir/rule' failed
make[1]: *** [CMakeFiles/mytests.dir/rule] Error 2
Makefile:175: recipe for target 'mytests' failed
make: *** [mytests] Error 2
*** Errore: Codice di uscita 2 ***
I have solved the problem by adding target_link_libraries(exampleopencl2 OpenCL) in the CMakeLists.txt file.

Error when trying to get the current process memory size on C++, MinGW

When I try to call GetProcessMemoryInfo I get an error:
undefined reference to `GetProcessMemoryInfo'
I've seen this issue:Undefined reference to getprocessmemoryinfo#12
but it doesn't solve mine.
I'm trying to know what is the size of my process in the RAM so to do that I need to use the 'GetProcessMemoryInfo' method.
My problem is that the link breaks when I do that.
CmakeLists.txt:
project(maxpath)
set(dir ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${dir}/build")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static-libgcc -static-libstdc++ -static -m64 -lpsapi")
file( GLOB LIB_ALG algorithms/*.hpp )
file( GLOB LIB_DS datastructures/*.hpp )
file( GLOB LIB_LOG include/*.h)
set(GRID_GENERATOR
${LIB_ALG}
${LIB_DS}
${LIB_LOG}
grid/generator.cpp
grid/grid.hpp)
set(GRID_SOLVER
${LIB_ALG}
${LIB_DS}
${LIB_LOG}
grid/main_grid.cpp
grid/grid.hpp
include/memory_helper.cpp include/memory_helper.hpp include/fnv.h)
add_executable(gridGenerator ${GRID_GENERATOR})
add_executable(gridSolver ${GRID_SOLVER})
You can see that I use the -lpsapi argument,
The error I get is:
[ 33%] Linking CXX executable "some path...\gridSolver.exe"
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0xf1): undefined reference to `GetProcessMemoryInfo'
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0x131): undefined reference to `GetProcessMemoryInfo'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj): bad reloc address 0x0 in section `.pdata'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [some path.../gridSolver.exe] Error 1
CMakeFiles\gridSolver.dir\build.make:121: recipe for target 'some path.../gridSolver.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/gridSolver.dir/all] Error 2
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/gridSolver.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/gridSolver.dir/rule] Error 2
CMakeFiles\Makefile2:115: recipe for target 'CMakeFiles/gridSolver.dir/rule' failed
mingw32-make.exe: *** [gridSolver] Error 2
makefile:130: recipe for target 'gridSolver' failed
I'm working with CLion and mingw-w64\x86_64-4.8.3-posix-seh-rt_v3-rev2\mingw64
Is there another way of doing this (except of using psapi)?
The answer there is correct. You should link against psapi:
FIND_LIBRARY (PSAPI Psapi)
TARGET_LINK_LIBRARIES(gridSolver ${PSAPI})
TARGET_LINK_LIBRARIES(gridGenerator ${PSAPI})
Or you can add it manually to the linker flags - you added it to the compiler flags in your example.

Error linking to Boost filesystem using cmake on cygwin

I'm using cmake 2.8.9, g++ 3.4.4, and Boost 1.50. in Cygwin on Windows 8 64 bit.
Here is the error message I get.
Linking CXX executable RayTracer.exe
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x89c):
undefined reference to boost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8a6):
undefined reference toboost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8b0):
undefined reference to boost::system::system_category()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o: bad reloc address 0xb in
section
.text$_ZN5boost6system14error_categoryD1Ev[boost::system::error_category::~error_category()]'
collect2: ld returned 1 exit status
CMakeFiles/RayTracer.dir/build.make:94: recipe for target
RayTracer.exe' failed make[2]: *** [RayTracer.exe] Error 1
CMakeFiles/Makefile2:64: recipe for target
CMakeFiles/RayTracer.dir/all' failed make[1]: *
[CMakeFiles/RayTracer.dir/all] Error 2 Makefile:75: recipe for target
`all' failed make: * [all] Error 2
From what I've seen, the usual problem is failing to link the boost system library, but I made sure to do that. Here is the relevant portion of my CMakeLists.txt file:
#Edit: cmake can't find the static libraries on cygwin, so I'm setting this to false for now.
SET(Boost_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(Boost 1.50 REQUIRED date_time program_options thread filesystem system unit_test_framework)
IF(${Boost_FOUND})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ENDIF()
add_executable(RayTracer
Ray_Tracer.cpp
)
target_link_libraries(RayTracer ${Boost_PROGRAM_OPTIONS_LIBRARIES})
And here's the line in my .cpp file that triggers the error:
#include <boost/filesystem.hpp>
Any idea what I'm doing wrong?
You need to tell the linker to link Boost.Filesystem and Boost.System libraries.
You can do:
target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:
target_link_libraries(RayTracer ${Boost_LIBRARIES})
For further details on the FindBoost CMake module, see the docs or run:
cmake --help-module FindBoost