Compiling a c++ including root libraries with g++ - c++

I am trying to compile a c++ program, which uses the TF1 library of the ROOT-framework, with the g++ compiler.
I tried
I already tried
g++ a.cpp -o 'root-config --cflags --glibs'
But that just gives me the error
g++: error: no such file or directory: 'root-config --cflags --glibs'
I am very new to both ROOT and C++ so help is very mucha appreciated!

You must check with:
g++ a.cpp $(root-config --cflags --glibs) -o a

Related

error in compiling a c++ code in order to use it in a django project

Recently I start a Django project in order to some image processing purposes. for some performance reasons, I wrote a function in c++ and test it independently. Now I want to call that c++ function in my python code. but when I compile it with this instruction:
g++ a.cpp `pkg-config --cflags --libs opencv` -shared -o program.so
this error occurred:
/usr/bin/ld: /tmp/ccvIlzBz.o: relocation R_X86_64_PC32 against symbol `_ZNK2cv7MatSizeclEv' can not be used when making a shared object; recompile with -fPIC
according to this error, if ّ replace -shared with -fPIC flag, it compiles but at my python code, this error occurred:
cannot open shared object file:
which means that it is not compiled correctly.
finally I build the c++ program with this instruction:
g++ a.cpp `pkg-config --cflags --libs opencv` -shared -fPIC -lm -o program.so
but when I execute my python code and call a function(sample_func) in that c++ code, it's give this error :
../program.so: undefined symbol: sample_func
how I can fix this problem?

arm-linux-gnueabi-g++ can't link xerces-c

arm-linux-gnueabi-g++ can't compile code that uses the xerces-c parser. Specifically, it can't seem to locate the xerces-c library even when I specify the full path with the I- flag or link it with -lxerces-c. However, when I compile with the generic g++ parser, everything works fine. Moreover, g++ works fine when I move around the xerces-c directories.
The code for g++ compilation:
g++ -pthread -g -c -std=c++0x src/MyFile.cpp -o $(TARGET_DIR)/MyFile.o -lxerces-c
The code for arm-linux-gnueabi-g++:
arm-linux-gnueabi-g++ -pthread -g -c -std=c++0x src/MyFile.cpp -o $(TARGET_DIR)/MyFile.o -lxerces-c
This is the error I receive:
src/myFile.cpp fatal error: xerces/util/PlatformUtils.hpp: No such file or directory
#include <xerces/util/Platform/utils.hpp
compilation terminated
I also tried removing the angle brackets enclosing the xerces library and replacing them with quotations so the path wouldn't get messed up.

Geany, g++ and SDL errors in compilation

So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).

How to use shared library via G++?

I have a library that named matrix and used in my program that named test.cpp.
I can generate and use static library successfully, but when I want to use it as a shared library, I receive the following error :
ap1019#sharifvm:~/the03-copy$ ls
matrix.cpp matrix.h test.cpp
ap1019#sharifvm:~/the03-copy$ g++ -c matrix.cpp
ap1019#sharifvm:~/the03-copy$ g++ -shared -Wl,-soname,matrix.so -o matrix.so matrix.o
ap1019#sharifvm:~/the03-copy$ ls
matrix.cpp matrix.h matrix.o matrix.so test.cpp
ap1019#sharifvm:~/the03-copy$ g++ test.cpp matrix.so
ap1019#sharifvm:~/the03-copy$ ./a.out
./a.out: error while loading shared libraries: matrix.so: cannot open shared object file: No such file or directory
ap1019#sharifvm:~/the03-copy$
Does anyone have any idea?
It is better to follow the naming convention for shared libraries.You are linking in a wrong manner.
Check out following for details:
g++ -L/home/username/matrix -Wall -o test test.cpp -lmatrix
http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

I'm having linking or compilation errors

I am using Netbeans for my C++ project. I compiled my program using make and ran into this error:
collect2: error: ld terminated with signal 11 [Segmentation fault], core dumped
Makefile:4: recipe for target 'barn' failed
make: *** [barn] Error 1
Whereas, when I compiled it in a linux environment(Ubuntu to be precise), it compiled fine. What could have possibly gone wrong?
This is what I got when I typed make -n:
g++ -c main.cc
g++ -c Animal.cc
g++ -c Bird.cc
g++ -c Chicken.cc
g++ -c Cat.cc
g++ -c Pig.cc
g++ -o barn main.o Animal.o Bird.o Chicken.o Cat.o Pig.o Random.o
PS I prefer using Netbeans
A segment fault in the linker suggests a bug with that. This is what I would try if I were to run into this problem.
At the command line do
g++ -o barn main.cc Animal.cc Bird.cc Chicken.cc Cat.cc Pig.cc Random.cc
If that does not work, try variations like:
g++ -o barn main.cc Pig.cc Random.cc Animal.cc Bird.cc Chicken.cc Cat.cc
The order should not matter. This is just the kind of thing I would try with a mystery-meat problem like this.