Compile CUDA code with cmake and 3.5 compute capability - c++

I need to compile a CUDA code that use a dynamic parallelism with cmake.
The code is:
#include <stdio.h>
__global__ void childKernel() {
printf("Hello ");
}
__global__ void parentKernel() {
childKernel<<<1,1>>>();
cudaDeviceSynchronize();
printf("World!\n");
}
int main(int argc, char **argv){
parentKernel<<<1, 1>>>();
return 0;
}
and the cmake is the following:
cmake_minimum_required(VERSION 2.8)
find_package(CUDA QUIET REQUIRED)
include_directories(/usr/include)
include_directories(/usr/local/cuda/lib)
include_directories(/usr/local/cuda-8.0/lib)
include_directories(/usr/local/cuda/include)
include_directories(/usr/local/cuda-8.0/include)
set(CUDA_SEPARABLE_COMPILATION ON)
find_package(CUDA QUIET REQUIRED)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
set(
CUDA_NVCC_FLAGS
${CUDA_NVCC_FLAGS};
-arch=compute_35 -rdc=true -lcudadevrt
)
cuda_add_executable(
prova
test.cu
)
I have tried to compile the code directly with nvcc passing the -arch=compute_35 -rdc=true -lcudadevrt and it compiles perfectly, but when
I try to compile with cmake it returns me the following error:
CMakeFiles/prova.dir/prova_intermediate_link.o: In function `__cudaRegisterLinkedBinary_66_tmpxft_00001101_00000000_13_cuda_device_runtime_compute_62_cpp1_ii_8b1a5d37':
link.stub:(.text+0xcc): undefined reference to `__fatbinwrap_66_tmpxft_00001101_00000000_13_cuda_device_runtime_compute_62_cpp1_ii_8b1a5d37'
link.stub:(.text+0xd0): undefined reference to `__fatbinwrap_66_tmpxft_00001101_00000000_13_cuda_device_runtime_compute_62_cpp1_ii_8b1a5d37'
collect2: error: ld returned 1 exit status
CMakeFiles/prova.dir/build.make:200: recipe for target 'prova' failed
make[2]: *** [prova] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/prova.dir/all' failed
make[1]: *** [CMakeFiles/prova.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

The undefined symbols you're seeing are defined in libcudadevrt.a. I see that you're telling CMake to link to it, but it appears the compiler can't find it. Try this on the command line:
VERBOSE=1 make
and inspect the output to see if you're searching /usr/local/cuda/lib64 for libraries.
This may be unrelated, but I also notice that you're instructing CMake to search /usr/local/cuda/lib and /usr/local/cuda-8.0/lib for header files. This is incorrect, as these directories contain only libraries.

Thanks for the answer, I have tried to use the correct compute capability of the Tegra X2 (compute_62), and I have inspected the output of verbose make and I found the following output:
CMakeFiles/prova.dir/prova_generated_test.cu.o CMakeFiles/prova.dir/prova_intermediate_link.o -o prova -rdynamic /usr/local/cuda-8.0/lib64/libcudart_static.a -lpthread -ldl -lrt
It seems that the linker try to search /usr/local/cuda-8.0/lib64/libcudart_static.a but not libcudadevrt.a

Related

ld.lld: error: could not open 'libLIBCMTD.a': No such file or directory

I recently installed vspkg and tried to build my c++ application with libcurl using command vcpkg.exe install curl:x64-windows-static
After i tried to compile it, i got an error on linking stage
ld.lld: error: could not open 'libLIBCMTD.a': No such file or directory
ld.lld: error: could not open 'libOLDNAMES.a': No such file or directory
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\testEnv.dir\build.make:140: C:/Users/Administrator/libtestEnv.dll] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/testEnv.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/testEnv.dir/rule] Error 2
mingw32-make: *** [Makefile:123: testEnv] Error 2
I also tried to install curl library non-static and everything went successfully but i want to have everything linked as one libary so it's not a good solution
My CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(testEnv)
# remove names of functions and optimize
set(CMAKE_CXX_FLAGS "-nolibc -s -O3 -Os -fdata-sections -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -fuse-ld=lld")
set(CMAKE_CXX_STANDARD 17)
find_package(CURL CONFIG REQUIRED)
add_library(testEnv SHARED main.cpp)
target_link_libraries(testEnv CURL::libcurl)
#collect all needed libraries to run
target_link_libraries(testEnv -static)
Any ideas how to fix that problem with linking? Maybe there is any solutions which would allow to exclude those problematic libs?
mingw32-make
looks like you are using mingw. Consider using the correct vcpkg triplet, e.g. x64-mingw-static.cmake.
x64-windows-static will use an installed VS toolchain.
Be aware that you also need to set -DVCPKG_TARGET_TRIPLET=x64-mingw-static and -DVCPKG_HOST_TRIPLET=x64-mingw-static in your cmake call. Also make sure cmake does clean configure.

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?

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.

Shared library on Linux and -fPIC error

I am trying to compile a shared library in Linux using a Makefile created with Cmake, but running make I obtain the following error:
Linking CXX shared library libcpp-lib.so
/usr/bin/ld: /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a(error_code.o): relocation R_X86_64_32 against .rodata.str1.1 can not be used when making a shared object; recompile with -fPIC
/home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[2]: *** [libcpp-lib.so] Error 1
make[1]: *** [CMakeFiles/cpp-lib.dir/all] Error 2
make: *** [all] Error 2
I provide the following command in the CMakeLists.txt in order to say that I want a shared (.so) library:
add_library(cpp-lib SHARED ${CPP_FILES})
What else do I need to specify in order to avoid the -fPIC error shown above?
Thanks a lot in advance
The boost libraries needs to be compiled using -fPIC:
Please have a look at: How to compile static library with -fPIC from boost.python
Try to add compiler flags by cmake by in your project:
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

Using cmake with share libraries

So I'm trying to get an example poco program running running with cmake. From here.
#include <Poco/String.h>
using Poco::trim;
using Poco::trimLeft;
using Poco::trimRight;
using Poco::trimRightInPlace;
#include <iostream>
int main(int argc, char** argv)
{
std::string hello(" This is a silly string. ");
std::cout << hello << std::endl;
std::string s1(trimLeft(hello));
trimRightInPlace(s1);
std::string s2(trim(hello));
std::cout << s2 << std::endl;
return 0;
}
I can compile and run the code from this command.
g++ -o silly silly.cpp -L/usr/local/lib -lPocoFoundation -I/usr/local/include/poco
But when I try to use cmake with a file like this.
cmake_minimum_required (VERSION 2.6)
project (Silly)
ADD_DEFINITIONS(
-std=c++11
)
# the version number
set (Silly_VERSION_MAJOR 0)
set (Silly_VERSION_MINOR 1)
include_directories (${PROJECT_BINARY_DIR})
include_directories ("/usr/local/include/poco")
include_directories ("/usr/local/lib")
# add the libs
set (EXTRA_LIBS ${EXTRA_LIBS} PocoFoundation)
# add the executable
add_executable (Silly silly.cpp)
target_link_libraries (Silly ${EXTRA_LIBS})
I get this error.
CMakeFiles/Silly.dir/silly.cpp.o: In function `Poco::Ascii::properties(int)':
silly.cpp:(.text._ZN4Poco5Ascii10propertiesEi[_ZN4Poco5Ascii10propertiesEi]+0x21): undefined reference to `Poco::Ascii::CHARACTER_PROPERTIES'
collect2: error: ld returned 1 exit status
make[2]: *** [Silly] Error 1
make[1]: *** [CMakeFiles/Silly.dir/all] Error 2
make: *** [all] Error 2
Here's my verbose output
/usr/bin/c++ -I/home/matt/projects/billy/build -I/usr/local/include/poco -I/usr/local/lib -std=c++11 -o CMakeFiles/Silly.dir/silly.cpp.o -c /home/matt/projects/billy/src/silly.cpp
Linking CXX executable Silly
/usr/bin/cmake -E cmake_link_script CMakeFiles/Silly.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Silly.dir/silly.cpp.o -o Silly -rdynamic -lPocoFoundation
CMakeFiles/Silly.dir/silly.cpp.o: In function `Poco::Ascii::properties(int)':
silly.cpp:(.text._ZN4Poco5Ascii10propertiesEi[_ZN4Poco5Ascii10propertiesEi]+0x21): undefined reference to `Poco::Ascii::CHARACTER_PROPERTIES'
collect2: error: ld returned 1 exit status
make[2]: *** [Silly] Error 1
make[2]: Leaving directory `/home/matt/projects/billy/build'
make[1]: *** [CMakeFiles/Silly.dir/all] Error 2
make[1]: Leaving directory `/home/matt/projects/billy/build'
make: *** [all] Error 2