(C++) glibmm won't link on Ubuntu/Oneiric - c++

I have problems clinking simplest program on Ubuntu/Oneiric:
#include <glibmm/ustring.h>
int main()
{
Glib::ustring s = "Test string";
}
using Makefile
PACKAGES=glibmm-2.4 glib-2.0 gtkmm-3.0 gtk+-3.0
CC=g++
CFLAGS=`pkg-config --cflags $(PACKAGES)` --std=c++0x
LD=g++
LDFLAGS=`pkg-config --libs $(PACKAGES)`
build: ./main
run: build
./main
clean:
rm ./main.o
rebuild: clean build
./main: ./main.o
$(LD) $(LDFLAGS) ./main.o -o ./main
./main.o: ./main.cc
$(CC) $(CFLAGS) ./main.cc -c -o ./main.o
on make following errors appears:
./main.o: In function `main':
main.cc:(.text+0x15): undefined reference to `Glib::ustring::ustring(char const*)'
main.cc:(.text+0x21): undefined reference to `Glib::ustring::~ustring()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
On Ubuntu/Maverick the exactly same code links well with exactly same file...
if using ld on main.o it links successfully too but (as it was expected) _start is missing...
Any suggestions?

Try changing the relevant lines to this:
LDFLAGS=`pkg-config --libs-only-L --libs-only-other $(PACKAGES)`
LIBS=`pkg-config --libs-only-l $(PACKAGES)`
# ...
./main: ./main.o
$(LD) $(LDFLAGS) ./main.o -o ./main $(LIBS)
The reason is that the linker may search libraries in the order they are given on the command line, so they should always be placed last to be safe.

Related

Issue with the linker using opencv and C++ - Undefined Reference

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.

Compile a c++ program on PC to communicate with DJI phantom 4 pro

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.

Makefile linking

I ran into an issue with make. I have 3 files.
main.cpp | src/Math/Vector2.cpp | src/Math/Vector2.hpp
Here is my MakeFile:
main: vector2.o main.o
g++ -o main.o vector2.o
main.o: main.cpp
g++ -o main.o main.cpp -c
vector2.o: src/Math/Vector2.cpp src/Math/Vector2.hpp
g++ -o vector2.o src/Math/Vector2.cpp -lm -c
When i copy these commands manually , it compiles perfectly fine.
However $make main returns
g++ main.cpp -o main
/tmp/ccnRZ4UD.o: In function `main':
main.cpp:(.text+0x42): undefined reference to `
phy2d::Maths::Vector2f::Vector2f(double, double)'
main.cpp:(.text+0x66): undefined reference to `
phy2d::Maths::Vector2f::Vector2f(double, double)'
main.cpp:(.text+0x79): undefined reference to `
phy2d::Maths::Vector2f::distance(phy2d::Maths::Vector2f const&)
const'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'main' failed
make: *** [main] Error 1
Any ideas??
There's no way that the makefile you provide will give the output you show.
In your comment you said Here is my MakeFile. Note that make will not read a file named MakeFile. It will read files named Makefile and makefile, but if you're using a case-sensitive filesystem then one explanation for the behavior you're seeing is that you've used MakeFile for your makefile name and make can't find it.
Or, you could have simply been imprecise in your question, but then this can't be the makefile that make is using for some other reason.
Also, there are numerous errors with your makefile:
You have two different targets main and main.o where the command generates the same file, -o main.o
You are adding libraries -lm to your compile line for vector2.o; libraries should go on the link line.
In general you should be using automatic variables to make sure your makefile is agreeing with what you want it to do.
Here's a reasonable makefile for your situation:
CXX = g++
main: vector2.o main.o
$(CXX) -o $# $^ -lm
main.o: main.cpp
$(CXX) -c -o $# $<
vector2.o: src/Math/Vector2.cpp src/Math/Vector2.hpp
$(CXX) -c -o $# $<

Compiling Module on Nao robot using custom makefile

I am programming a module for my Alderbaran Nao V5 robot. Alderbaran recommends using qibuild to compile a module, and I was able to successfully do so, but now I am trying to migrate towards writing my own makefile and using g++.
I am encountering the following error:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.3/../../../../lib/crt1.o: In function
`_start':
(.text+0x18): undefined reference to `main'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqimessaging.so: undefined reference to `std::__detail::_List_node_base::swap(std::__detail::_List_node_base&, std::__detail::_List_node_base&)#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqi.so: undefined reference to `std::invalid_argument::~invalid_argument()#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqitype.so: undefined reference to `std::out_of_range::~out_of_range()#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqitype.so: undefined reference to `std::__detail::_List_node_base::_M_transfer(std::__detail::_List_node_base*, std::__detail::_List_node_base*)#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqimessaging.so: undefined reference to `std::__detail::_List_node_base::_M_unhook()#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqimessaging.so: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)#GLIBCXX_3.4.15'
/home/nao/naoqi-sdk-2.1.3.3-linux32/lib/libqi.so: undefined reference to `posix_spawnp#GLIBC_2.15'
collect2: ld returned 1 exit status
make: *** [shm.so] Error 1
Here is the makefile I am using:
#Variables
CXXFLAGS=-Wall -g
CXX = g++
NaoQi_INC = /home/nao/naoqi-sdk-2.1.3.3-linux32/include
NaoQi_LIB = -L/home/nao/naoqi-sdk-2.1.3.3-linux32/lib -lalcommon -lalerror -lalproxies -lalvalue -lqimessaging -lqitype -lqi
#Object Targets
main.o: main.cpp shm.h
$(CXX) $(CXXFLAGS) -c -I$(NaoQi_INC) main.cpp
shm.o: shm.cpp shm.h $(NaoQi_INC)/alcommon/alproxy.h $(NaoQi_INC)/alcommon/albroker.h $(NaoQi_INC)/alcommon/almodule.h
$(CXX) $(CXXFLAGS) -c -I$(NaoQi_INC) shm.cpp
#Library Targets
shm.so: main.o shm.o
$(CXX) $(CXXFLAGS) -o shm.so main.o shm.o -L/usr/local/lib -lm $(NaoQi_LIB)
clean:
rm -f *o main
rm -f *o shm
all: shm.o main.o shm.so
I noticed that the gentoo operating system I am compiling on has up to GLIBCXX_3.4.14 but does not have GLIBCXX_3.4.15. How can I fix this issue? Why would this issue not present itself when I build the module using qibuild?
Interestingly, the problem was that my .so linking command was missing a -shared flag. Not sure why that gave all of the errors I ran into, however.

Statically link against pocketsphinx (Library)

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.