Unsolved CUDA and QT create library
-
Hello i try to use CUDA with QT and get some errors.
This example work fine:
main.cppextern "C" void runKernel(); #include <cstdio> int main(int argc, char **argv) { runKernel(); printf("Kernel passed \n"); return 0; }
#include <cstdio> __global__ void testKernel() { } extern "C" void runKernel() { testKernel<<<1,1>>>(); printf("\n Kernel pass \n"); }
Build instructions
nvcc -ccbin g++ -m64 -arch=sm_50 -dc -o MWE.o -c MWE.cu nvcc -ccbin g++ -m64 -arch=sm_50 -dlink -o MWE.dlink.o MWE.o nvcc -ccbin g++ -m64 -arch=sm_50 -lib -o MWE.a MWE.o MWE.dlink.o g++ -m64 -o main.o -c main.cpp g++ -m64 main.o MWE.a -o test -L/usr/local/cuda/lib64 -lcudart
So okay I get library MWE.a and can compile with main.cpp using g++, I try to create new library with previous one MWE.a
KL.pro#------------------------------------------------- # # Project created by QtCreator 2019-03-18T19:09:38 # #------------------------------------------------- TEMPLATE = lib CONFIG -= qt CONFIG += console CONFIG -= app_bundle CONFIG += c++11 INCLUDEPATH += home/kyll/Documents/CUDATEST/LAST LIBS += -L/home/kyll/Documents/CUDATEST/LAST/MWE.a # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ kl.cpp HEADERS += \ kl.h CONFIG(debug, debug|release) { # debug unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -fopenmp } } else { # release unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp } } CONFIG (debug, debug|release) { OBJECTS_DIR = debug } else { OBJECTS_DIR = release }
kl.h
#ifndef KL_H #define KL_H extern "C" void runKernel(); extern "C" void f(); #endif // KL_H
kl.cpp
#include <cstdio> #include "kl.h" void runKernel(); void f() { runKernel(); printf("Test \n"); }
MinGW generating library .so , .so.1 , .so.1.0, .so.1.0.0, I use .so in my app and get errors:
:-1: error: /tmp/ccCGR1vb.ltrans0.ltrans.o: in function `main':
:-1: error: collect2: error: ld returned 1 exit status#------------------------------------------------- # # Project created by QtCreator 2019-03-18T19:09:38 # #------------------------------------------------- TEMPLATE = app CONFIG -= qt CONFIG += console CONFIG -= app_bundle CONFIG += c++11 INCLUDEPATH += /home/kyll/Documents/CUDATEST/LAST/build-KL-Desktop_Qt_5_10_0_GCC_64bit-Debug LIBS += -L/home/kyll/Documents/CUDATEST/LAST/build-KL-Desktop_Qt_5_10_0_GCC_64bit-Debug/libKL.so # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ lm.cpp HEADERS += \ lm.h CONFIG(debug, debug|release) { # debug unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -fopenmp } } else { # release unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp } } CONFIG (debug, debug|release) { OBJECTS_DIR = debug } else { OBJECTS_DIR = release }
lm.h
#ifndef LM_H #define LM_H extern "C" void runKernel(); extern "C" void f(); #endif // LM_H
lm.cpp
#include <cstdio> #include "lm.h" int main(){ runKernel(); //f(); return 0; }
What I misunderstand? Where error?
-
Hi and welcome to devnet,
-L
is for giving an additional search path to the linker.You need to use
-l
(lowercase L) to give an additional library to use to the linker. -
I change few lines in KL.pro:
INCLUDEPATH += home/kyll/Documents/CUDATEST/LAST LIBS += -L/home/kyll/Documents/CUDATEST/LAST -lMWE
add -l and name of library and get errors
:-1: error: cannot find -lMWE
:-1: error: collect2: error: ld returned 1 exit status -
Add external library using project -> add library (recompile with new name lib*.a)
unix:!macx: LIBS += -L$$PWD/../ -lMWE INCLUDEPATH += $$PWD/../ DEPENDPATH += $$PWD/../ unix:!macx: PRE_TARGETDEPS += $$PWD/../libMWE.a
Now get new errors:
:-1: error: /home/kyll/Documents/CUDATEST/LAST/KL/..//libMWE.a(MWE.o): relocation R_X86_64_PC32 against symbol `_Z10testKernelv' can not be used when making a shared object; recompile with -fPIC
:-1: error: final link failed: bad value
:-1: error: collect2: error: ld returned 1 exit statusRecompile with -fPIC all work.(Can build library in library)
But try to use this library in LM.pro I get errors
:-1: error: /tmp/ccB1sXku.ltrans0.ltrans.o: in function `main':
:-1: error: collect2: error: ld returned 1 exit status -
Are you using a self-built Qt version ?
-
@SGaist I install qt (opensource version) from maintenance tool.
-
How did you add
-fPIC
to your library build ? -
-
Okay I can make this:
main.cppextern "C" void runKernel(); #include <cstdio> int main(int argc, char **argv) { runKernel(); printf("Kernel passed\n"); return 0; }
#include <cstdio> __global__ void testKernel() { } extern "C" void runKernel() { testKernel<<<1,1>>>(); printf("\nKernel pass \n"); }
Build
nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -dc -o MWE.o -c MWE.cu nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -dlink -o MWE.dlink.o MWE.o nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -lib -o libMWE.a MWE.o MWE.dlink.o
TEMPLATE = lib CONFIG -= qt CONFIG += console CONFIG -= app_bundle CONFIG += c++11 DEFINES += QT_DEPRECATED_WARNINGS CUDA_DIR = /usr/local/cuda-10.1 INCLUDEPATH += $$CUDA_DIR/include QMAKE_LIBDIR += $$CUDA_DIR/lib64 LIBS += -L$$CUDA_DIR/lib64 -lcudart -lcuda SOURCES += \ kl.cpp CONFIG(debug, debug|release) { # debug unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -fopenmp } } else { # release unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp } } CONFIG (debug, debug|release) { OBJECTS_DIR = debug } else { OBJECTS_DIR = release } unix:!macx: LIBS += -L$$PWD/../ -lMWE INCLUDEPATH += $$PWD/../ DEPENDPATH += $$PWD/../ unix:!macx: PRE_TARGETDEPS += $$PWD/../libMWE.a
KL.cpp
#include <cstdio> extern "C" void runKernel(); extern "C" void f() { runKernel(); printf("Test \n"); }
TEMPLATE = app CONFIG -= qt CONFIG += console CONFIG -= app_bundle CONFIG += c++11 DEFINES += QT_DEPRECATED_WARNINGS CUDA_DIR = /usr/local/cuda-10.1 INCLUDEPATH += $$CUDA_DIR/include QMAKE_LIBDIR += $$CUDA_DIR/lib64 LIBS += -L$$CUDA_DIR/lib64 -lcudart -lcuda SOURCES += \ lm.cpp CONFIG(debug, debug|release) { # debug unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -fopenmp } } else { # release unix: { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp } else { QMAKE_LFLAGS = -fopenmp QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp } } CONFIG (debug, debug|release) { OBJECTS_DIR = debug } else { OBJECTS_DIR = release } unix:!macx: LIBS += -L$$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release/ -lKL INCLUDEPATH += $$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release DEPENDPATH += $$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release
LM.cpp
#include <cstdio> extern "C" void runKernel(); extern "C" void f(); int main(){ runKernel(); f(); return 0; }