I have been searching everywhere for a solution to my problem:
I cannot run a .cpp file using CUDA. I think this is a module error since I get the following error:
g++ -L/usr/local/cuda/lib64 -L~/NVIDIA_GPU_Computing_SDK/shared/lib/linux -L~/NVIDIA_GPU_Computing_SDK/C/common/lib/linux -L~/NVIDIA_GPU_Computing_SDK/C/lib -lcutil -lcudpp -lcuda -lcudart -lcurand -o my_file my_file.o
/usr/bin/ld: cannot find -lcutil
/usr/bin/ld: cannot find -lcudpp
My makefile looks like this:
EXECUTABLE := my_file
SDKPATH := ~/NVIDIA_GPU_Computing_SDK
CUDAPATH := /usr/local/cuda
LDFLAGS := -L$(CUDAPATH)/lib64 -L$(SDKPATH)/shared/lib/linux -L$(SDKPATH)/C/common/lib/linux -L$(SDKPATH)/C/lib -lcutil -lcudpp -lcuda -lcudart -lcurand
CXFLAGS := -I$(CUDAPATH)/include -I$(SDKPATH)/shared/inc -I$(SDKPATH)/C/common/inc
CXX := g++
NVCC := $(CUDAPATH)/bin/nvcc
$(EXECUTABLE): my_file.o
$(CXX) $(LDFLAGS) -o $(EXECUTABLE) my_file.o
my_file.o: my_file.cu
$(NVCC) $(CXFLAGS) -c my_file.cu
When I, instead try to run the file manually I get the following output:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
my_file.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_00000e8f_00000000-1_my_file.cudafe1.cpp:(.text+0xf): undefined reference to `__cudaUnregisterFatBinary'
my_file.o: In function `__sti____cudaRegisterAll_59_tmpxft_00000e8f_00000000_4_my_file_cpp1_ii_71dc03a4()':
tmpxft_00000e8f_00000000-1_my_file.cudafe1.cpp:(.text+0x1f): undefined reference to `__cudaRegisterFatBinary'
collect2: ld returned 1 exit status
I'm a total noob when it comes to linux.
Can anyone please shed some light on this situation.
Regards
You have an ordering problem in the linking phase of your build. Because the libraries you specify are provided before the files that require them, they get discarded. If you change your makefile to something like:
LDFLAGS := -L$(CUDAPATH)/lib64 -L$(SDKPATH)/shared/lib/linux -L$(SDKPATH)/C/common/lib/linux
LIBS := -lcutil -lcudpp -lcuda -lcudart -lcurand
....
$(EXECUTABLE): my_file.o
$(CXX) $(LDFLAGS) -o $(EXECUTABLE) my_file.o $(LIBS)
you should find the problem goes away.
Related
There are a couple similar problems to this but they all seem to have answers that don't work.
I've installed opencv4 onto an image of Ubuntu and I know the opencv files are install correctly
$ pkg-config --cflags opencv
-I/usr/include/opencv
and have a proper .cp file
/usr/local/lib/pkgconfig$ ls
gpr.pc grpc++.pc grpc++_unsecure.pc protobuf-lite.pc
grpc.pc grpc_unsecure.pc opencv4.pc protobuf.pc
But even though it's all installed correctly it's giving me this error.
~/neuralink/neuralink-image-service-prompt/proto$ make g++ -std=c++11 `pkg-config --cflags opencv protobuf grpc` -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl foo.cpp -o foo
/tmp/ccnfoOEl.o: In function `main':
foo.cpp:(.text+0x48): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccnfoOEl.o: In function `cv::String::String(char const*)':
foo.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccnfoOEl.o: In function `cv::String::~String()':
foo.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::String::operator=(cv::String const&)':
foo.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccnfoOEl.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'foo' failed
make: *** [foo] Error 1
Some people have already tried answering similar questions (such as Undefined reference to cv::imread(cv::String const&, int) and linking opencv libraries with g++) however these don't help, as the user didn't add opencv to their makefile and I have.
Here is my make makefile
LDFLAGS = -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
CXX = g++
CPPFLAGS += `pkg-config --cflags opencv protobuf grpc`
CXXFLAGS += -std=c++11
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
all: foo
client: image.pb.o image.grpc.pb.o client.o
$(CXX) $^ $(LDFLAGS) -o $#
server: image.pb.o image.grpc.pb.o server.o
$(CXX) $^ $(LDFLAGS) -o $#
%.grpc.pb.cc: %.proto
protoc --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
protoc --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h client server
And I wrote a simple program that still throws the error
#include <opencv2/opencv.hpp>
#include <stdio.h>
int main() {
cv::Mat frame = cv::imread("sylvania.png", cv::IMREAD_UNCHANGED);
printf("Image size is %i x %i\n", frame.rows, frame.cols);
}
Everything seems to compile fine but I keep getting this linker issue. I don't seem to get the issue when I compile it on another computer (my mac) for what it's worth. Also I have the gRPC and Protoc files because I need them for the project, I just didn't use those file as an example since they're quite long.
I agree with Micka, here is a Makefile of one of my projects:
CPPFLAGS=-g -Wall -I. -DDEBUG
LDFLAGS=-g
LDLIBS=-lopencv_core -lopencv_calib3d -lopencv_highgui -lopencv_imgproc -lopencv_stitching -lopencv_video
main: main.o AffineKalmanLP.o
g++ $(LDFLAGS) -o main main.o AffineKalmanLP.o $(LDLIBS)
main.o: main.cpp main.h
g++ $(CPPFLAGS) -c main.cpp
AffineKalmanLP.o: AffineKalmanLP.cpp AffineKalmanLP.h
g++ $(CPPFLAGS) -c AffineKalmanLP.cpp
clean:
rm main main.o AffineKalmanLP.o
You need to add it to LDLIBS.
If I remove that line, I get undefined reference for various things as well.
For context, I'm trying to compile source to a 32-bit executable for Windows using a Linux machine. I'm using the current mingw-w64 via apt-get. Here's the project I'm trying to compile ftp://ftp.thegpm.org/projects/tandem/source. More specifically the 17-02-01 zip files contain the source I'm interested in.
My first attempt was to just edit with the Makefile_ubuntu under the tandem-linux and swap out the gcc with the one provided by mingw and fix header reference issues that cropped up by adding #includes to .cpp files that threw errors. Super hacky. Can someone show me a brighter path?
Here's the makefile I'm using:
#makefile for c++ programs
#change the name of the executable to use for "any" project
EXECUTABLE = ../bin/tandem.exe
#EXECUTABLE = ../bin/p3.exe
LINKCC = $(CXX)
#CXXFLAGS denotes flags for the C++ compiler
CXX = /usr/bin/i686-w64-mingw32-g++-win32
#uncomment this line if you are using gcc 4.x
CXXFLAGS = -m32 -std=gnu++11
#CXXFLAGS = -w -O2 -DGCC4_3
#CXXFLAGS = -w -O2 -DGCC4_3 -DX_P3
#ubuntu 64 bit version
#LDFLAGS = -L/usr/lib/x86_64-linux-gnu/libexpat.a
LDFLAGS = -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a
#LDFLAGS = -lpthread -L/usr/lib -lm -lexpat
SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
DEPS := $(patsubst %.o,%.d,$(OBJS))
all: $(EXECUTABLE)
#define the components of the program, and how to link them
#these components are defined as dependencies; that is they must be up-to-date before the code is linked
$(EXECUTABLE): $(DEPS) $(OBJS)
$(LINKCC) $(CXXFLAGS) -o $(EXECUTABLE) $(OBJS) $(LDFLAGS)
#specify the dep files depend on the cpp files
%.d: %.cpp
$(CXX) -M $(CXXFLAGS) $< > $#
$(CXX) -M $(CXXFLAGS) $< | sed s/\\.o/.d/ > $#
clean:
-rm $(OBJS) $(EXECUTABLE) $(DEPS) *~
explain:
#echo "The following info represents the program:"
#echo "Final exec name: $(EXECUTABLE)"
#echo "Source files: $(SRCS)"
#echo "Object files: $(OBJS)"
#echo "Dep files: $(DEPS)"
depend: $(DEPS)
#echo "Deps are now up-to-date."
-include $(DEPS)
And here is the error(s):
sudo make -f Makefile_ubuntu
/usr/bin/i686-w64-mingw32-g++-win32 -m32 -std=gnu++11 -o ../bin/tandem.exe tandem.o p3mprocess.o saxmzdatahandler.o mspectrumcondition.o masscalc.o mprocess.o mreport.o mscore_tandem.o loadmspectrum.o mplugin.o msequenceserver.o saxtaxhandler.o msequencecollection.o mscore.o mrefine.o xmltaxonomy.o mbiomlreport.o saxtandeminputhandler.o saxhandler.o msequtilities.o base64.o saxmodhandler.o mtermmods.o xmlparameter.o saxsaphandler.o saxmzxmlhandler.o saxmzmlhandler.o mxxcleavage.o p3msequenceserver.o mzid_report.o saxbiomlhandler.o p3.o mpmods.o saxgamlhandler.o stdafx.o MSNumpress.o mpam.o -lpthread -lm -L/usr/lib/x86_64-linux-gnu/libexpat.a
saxhandler.o:saxhandler.cpp:(.text+0x100): undefined reference to `XML_ParserCreate'
saxhandler.o:saxhandler.cpp:(.text+0x11d): undefined reference to `XML_SetUserData'
saxhandler.o:saxhandler.cpp:(.text+0x13b): undefined reference to `XML_SetElementHandler'
saxhandler.o:saxhandler.cpp:(.text+0x151): undefined reference to `XML_SetCharacterDataHandler'
saxhandler.o:saxhandler.cpp:(.text+0x1cf): undefined reference to `XML_ParserFree'
saxhandler.o:saxhandler.cpp:(.text+0x344): undefined reference to `XML_Parse'
saxhandler.o:saxhandler.cpp:(.text+0x37f): undefined reference to `XML_Parse'
saxhandler.o:saxhandler.cpp:(.text+0x3bd): undefined reference to `XML_GetErrorCode'
saxhandler.o:saxhandler.cpp:(.text+0x3d4): undefined reference to `XML_GetCurrentLineNumber'
collect2: error: ld returned 1 exit status
Makefile_ubuntu:33: recipe for target '../bin/tandem.exe' failed
make: *** [../bin/tandem.exe] Error 1
You are (incorrectly) linking with /usr/lib/x86_64-linux-gnu/libexpat.a which is a Linux library (in ELF format). You need to get some Windows version of it.
BTW -L/usr/lib/x86_64-linux-gnu/libexpat.a is incorrect. Since -L should give a directory not a library to link
At last, recent versions of Windows might have WSL which could be useful to you (you'll compile a Linux, mostly statically linked, executable, and it might run on the command line on Windows).
I am trying to receive on my PC(linux mint), a streaming from the camera of a DJI phantom 4 using OpenCV.
The example I am following is : https://developer.dji.com/guidance-sdk/documentation/tutorials/index.html (I am following the linux part)
I copied the Copy libDJI_guidance.so in /usr/local/lib and I checked, it is there.
The makefile is :
#define a compiler
CXX = g++
#define target name
TARGET = main
#define dependencies of target
OBJECTS = main.o DJI_utility.o
#define the Include and Library path
CFLAGS = -g -Wall -I/usr/local/include -I../../../include
LDFLAGS = -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
$(TARGET) : $(OBJECTS)
$(CXX) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
main.o : main.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c main.cpp DJI_utility.h
DJI_utility.o : DJI_utility.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c DJI_utility.cpp DJI_utility.h
clean:
rm -rf *.o *.gch *.avi $(TARGET)
But when i excute a make in the command line i get:
g++ -o main main.o DJI_utility.o -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
/usr/bin/ld: skipping incompatible /usr/local/lib//libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible /usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible //usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: cannot find -lDJI_guidance
/usr/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'main' failed
make: *** [main] Error 1
The project is located in:
~/Documents/studies_SRT/SRT5/TX_drone/Guidance-SDK/demo/guidance_track
The output of an ls is:
DJI_guidance.h DJI_utility.cpp DJI_utility.h DJI_utility.h.gch DJI_utility.o main.cpp main.o Makefile
Thank you.
You seem to have downloaded library files for the wrong architecture. If you're on a 64-bit system, download the 64-bit version of the libraries.
I am developing a little program with pocketsphinx (speech to text - library).
On Windows i was using Code::Blocks as development environment and i had success to build a program.
Now i try to port my program to Linux and i am having little problems to link against pocketsphinx.
This is the Makefile:
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx
OBJ = obj/Application.o obj/Main.o obj/Recorder.o
all: $(OBJ)
$(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx
obj/Main.o: src/Main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o
obj/Application.o: src/Application.cpp src/Application.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o
obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o
It is the same which i was using on Windows, i just adjusted the file path.
I am receiving the following error:
$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1
I don't think that it is a name mangling problem, since i built the lib on my own using the provided Makefile.
What can i do to link against the lib without errors?
EDIT: I figured out how to make it work. I simply modified the rule of the target "all" to this:
$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s -lpocketsphinx -lsphinxbase -lsphinxad -lpthread
Functions like ad_read are defined in libsphinxad library, you need to add it to your linker command line:
g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad
Please note that the order of libraries is important.
i have a small problem with OpenCL/OpenGL interoperability in my code
here's my makefile
LIBS = -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
CFLAGS = -Wall -g
OBJECTS = main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o
all: prognonmpi
prognonmpi: $(OBJECTS)
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ $(CFLAGS) -o prognonmpi $(OBJECTS) $(LIBS)
%.o: %.cpp $(LIBS)
clean:
rm -f *.o prog cmodules/*.o
the code just comiles fine, but i get a linker error with the following message
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ -Wall -g -o prognonmpi main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
main.o: In function `displayFunc()':
main.cpp:(.text+0xdda): undefined reference to `clEnqueueAcquireGLObjects'
main.cpp:(.text+0xe12): undefined reference to `clEnqueueReleaseGLObjects'
main.o: In function `initOpenGLBuffers(int, int)':
main.cpp:(.text+0x136a): undefined reference to `clCreateFromGLBuffer'
collect2: ld returned 1 exit status
make: *** [prognonmpi] Error 1
the code compiles very fine on my notebook with the lins, using -lGL instead of -lGAL, seesm -lGAL is ARM specific.
on the ARM machine i get the above mentioned error.
Any suggestions what is missing on my ARM-machine?
using Ubuntu as OS