I am using a code from someone else. When compiling it I got the following error: " référence indéfinie vers « CUtil::CUtil() ".
I guess I am missing a package or library? Or is it a problem with my compiler? (gcc version 5.4.0).
I am on ubuntu.
Any idea how I should procees to be able to compile?
Edit: it is a linkage issue. I have all the files I need in mu directory and here is the script to compile:
g++ -c -Wall -fPIC GeoConvert.cpp
g++ -c -Wall -fPIC GlobalFunctions.cpp
g++ -c -Wall -fPIC Util.cpp
g++ -c -Wall -fPIC TbwInput.cpp
g++ -shared -o libTbwInput.so GeoConvert.o GlobalFunctions.o Util.o TbwInput.o
g++ -c -Wall -fPIC Atmosphere.cpp
g++ -c -Wall -fPIC GlobalFunctions.cpp
g++ -c -Wall -fPIC Interface.cpp
g++ -c -Wall -fPIC WriteInputFile.cpp
g++ -shared -o libInterface.so Atmosphere.o GlobalFunctions.o Interface.o WriteInputFile.o
g++ -c -Wall -fPIC CalllTBWinterface.cpp
#g++ -c -Wall -fPIC stdafx.cpp
g++ -shared -o libTBWinterfaceWrapper.so CalllTBWinterface.o #stdafx.o
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:'/home/bomble/TIARE/HasanKhan/MDO_GITHUB/All Libraries/1.TBWinterfaceWrapper/'
g++ -o TBWinterfaceWrapper TBWinterfaceWrapper.cpp -L'/home/bomble/TIARE/HasanKhan/MDO_GITHUB/All Libraries/1.TBWinterfaceWrapper' -lTbwInput -lInterface -lTBWinterfaceWrapper
All but the last step go fine. And then I have error about things not find even if they are in the cpp compiled at the beginning....
Related
I am trying to run makefile but I cant make him find the files I add to it unless I put full path of the file: /home/"user_name"/CLionProjects/Ass1/src/main.cpp
I try to run the makefile when I am in Ass1 so as much that I understand the makefile should find the main.cpp .
Would love to know where I am wrong here, thank you.
what I write:
all: clean compile link
link:
g++ -o /bin/main /home/daniel/CLionProjects/Ass1/bin/main.o /home/daniel/CLionProjects/Ass1/bin/Workout.o /home/daniel/CLionProjects/Ass1/bin/Trainer.o /home/daniel/CLionProjects/Ass1/bin/Studio.o
compile:
g++ -g -Wall -Weffc++ -c -o bin/main.o /home/daniel/CLionProjects/Ass1/src/main.cpp
g++ -g -Wall -Weffc++ -c -o bin/Studio.o /home/daniel/CLionProjects/Ass1/src/Studio.cpp
g++ -g -Wall -Weffc++ -c -o bin/Workout.o /home/daniel/CLionProjects/Ass1/src/Workout.cpp
g++ -g -Wall -Weffc++ -c -o bin/Trainer.o /home/daniel/CLionProjects/Ass1/src/Trainer.cpp
clean:
rm -f bin/*
what I want to write and cant:
all: clean compile link
link:
g++ -o /bin/main /bin/main.o /bin/Workout.o /bin/Trainer.o /bin/Studio.o
compile:
g++ -g -Wall -Weffc++ -c -o bin/main.o /src/main.cpp
g++ -g -Wall -Weffc++ -c -o bin/Studio.o /src/Studio.cpp
g++ -g -Wall -Weffc++ -c -o bin/Workout.o /src/Workout.cpp
g++ -g -Wall -Weffc++ -c -o bin/Trainer.o /src/Trainer.cpp
clean:
rm -f bin/*
the error I get:
rm -f bin/*
g++ -g -Wall -Weffc++ -c -o bin/main.o /src/main.cpp
g++: error: /src/main.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [makefile:7: compile] Error 1
I am trying to compile a Pybind11 module in C++ which calls several header files (.h) on top. As I have a lot of header files, I decided to do a Makefile, which works without problem, EXCEPT for creating the target shared object file (s.o). I need this shared object file in order to be able to call the Pybind module in Python.
However, when compiling, I get:
g++ -shared -fPIC neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o population.o example.o -o example.so
/usr/bin/ld: example.o: relocation R_X86_64_PC32 against symbol `_ZTI3Pet' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'example.so' failed
make: *** [example.so] Error 1
My question is basically: What I am doing wrong when compiling the object files in order to create a target ?
Makefile
example.so: neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o population.o example.o
g++ neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o population.o example.o -shared -o example.so
neat.o: neat.cpp neat.h
g++ -c -O3 -Wall -fPIC neat.cpp
network.o: network.cpp network.h
g++ -c -O3 -Wall -fPIC network.cpp
nnode.o: nnode.cpp nnode.h
g++ -c -O3 -Wall -fPIC nnode.cpp
link.o: link.cpp link.h
g++ -c -O3 -Wall -fPIC link.cpp
trait.o: trait.cpp trait.h
g++ -c -O3 -Wall -fPIC trait.cpp
gene.o: gene.cpp gene.h
g++ -c -O3 -Wall -fPIC gene.cpp
innovation.o: innovation.cpp innovation.h
g++ -c -O3 -Wall -fPIC innovation.cpp
organism.o: organism.cpp organism.h genome.h genome.cpp species.h species.cpp
g++ -c -O3 -Wall -fPIC organism.cpp
species.o: species.h species.cpp organism.cpp organism.h genome.h genome.cpp
g++ -c -O3 -Wall -fPIC species.cpp
genome.o: genome.cpp genome.h
g++ -c -O3 -Wall -fPIC genome.cpp
population.o: population.cpp population.h organism.h
g++ -c -O3 -Wall -fPIC population.cpp
experiments.o: experiments.cpp experiments.h
g++ -c -O3 -Wall -fPIC experiments.cpp
example.o:
g++ -O3 -Wall -std=c++11 -fopenmp -I -fPIC `python3 -m pybind11 --includes` -c example.cpp
clean:
rm *.o *.so
example.cpp
#include <pybind11/pybind11.h>
#include <iostream>
#include <string>
#include "population.h"
namespace py = pybind11;
int create_neat(){
Population *the_pop=0;
return 0;
}
PYBIND11_MODULE(example, m){
m.def("create_neat", &create_neat, "create a pop object");
}
The standard doesn't completely define the difference between #include "..." and #include <...>, but in all compilers I'm familiar with the <> syntax for include typically means, "this is a system-type header file" and the "" syntax means "this is a local-type header file".
In practice this usually means that "" are looked for in the working directory, then in directories specified by -I options to the compiler and finally in system default directories, while <> is the same except it does not look in the working directory.
So, you either need to change your code to use:
#include "population.h"
or else you need to add -I. to your compile line so the compiler knows to look in the current directory:
population.o: population.cpp population.h organism.h
g++ -I. -c population.cpp
Personally I would do both, because it's good hygiene to add the directories you need to your compile line, and because it's good practice to use "" for local headers so people reading the code understand immediately that this is your header and not a system header.
For your second question, Is the compilation in order to make a shared object file with the object files correct I'm sorry but I don't understand what you're asking.
If you're asking about the order of targets defined in the makefile, then they can be in any order at all except that the first target will be the default target (if you run make with no target name then make will build the first target--and any prerequisites needed for the first target).
ETA
OK, now that we can see that the actual error is that example.o does not exist. Why does it not exist? Again, because you've removed important details from the example in your question we can't say for sure. When you write in your question:
example.so: //some .o files which are not important// HERE IS THE PROBLEM
what exactly are the .o files that appear as prerequisites of example.so?
You must list the file example.o as a prerequisite of example.so if you want make to build example.o before trying to build example.so. You need to list all the object files that are needed to create the shared library. So this should be:
example.so: population.o example.o neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o
It doesn't matter what order they appear in the prerequisites list, but they must all be there.
After several hours of reading, trying, and also thanks to the hints of MadScientist, I got it working but the reasons why are still unclear. I just changed the order of the prerequisites in the compilation of the target, by putting the -shared -o example.so in front and not at the end of the line, and it worked.
The whole file is as follows:
example.so: neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o population.o example.o
g++ -shared -o example.so neat.o network.o nnode.o link.o trait.o gene.o innovation.o organism.o species.o genome.o population.o example.o
neat.o: neat.cpp neat.h
g++ -c -O3 -Wall -fPIC neat.cpp
network.o: network.cpp network.h
g++ -c -O3 -Wall -fPIC network.cpp
nnode.o: nnode.cpp nnode.h
g++ -c -O3 -Wall -fPIC nnode.cpp
link.o: link.cpp link.h
g++ -c -O3 -Wall -fPIC link.cpp
trait.o: trait.cpp trait.h
g++ -c -O3 -Wall -fPIC trait.cpp
gene.o: gene.cpp gene.h
g++ -c -O3 -Wall -fPIC gene.cpp
innovation.o: innovation.cpp innovation.h
g++ -c -O3 -Wall -fPIC innovation.cpp
organism.o: organism.cpp organism.h genome.h genome.cpp species.h species.cpp
g++ -c -O3 -Wall -fPIC organism.cpp
species.o: species.h species.cpp organism.cpp organism.h genome.h genome.cpp
g++ -c -O3 -Wall -fPIC species.cpp
genome.o: genome.cpp genome.h
g++ -c -O3 -Wall -fPIC genome.cpp
population.o: population.cpp population.h organism.h
g++ -c -O3 -Wall -fPIC population.cpp
experiments.o: experiments.cpp experiments.h
g++ -c -O3 -Wall -fPIC experiments.cpp
example.o:
g++ -O3 -Wall -std=c++11 -fopenmp `python3 -m pybind11 --includes` -fPIC -c example.cpp
clean:
rm *.o *.so
When I try to use the cplex from IBM in my code and try to compile it, it shows the following error. I've provided the path to the cplex include file and the concert include file, but it still says 'ilcplex/ilocplex.h' file not found.
test: test.o assembly.pb.o Mesh.o Assembly.o Part.o Mate.o MateConnector.o ConstraintsMath.o ConnectGraph.o Model.o myOptimizer.o
g++ test.o assembly.pb.o Mesh.o Assembly.o Part.o Mate.o MateConnector.o ConnectGraph.o ConstraintsMath.o Model.o myOptimizer.o -o test -lprotobuf
test.o: test.cc assembly.pb.h Assembly.h Mate.h Model.h Mesh.h Part.h MateConnector.h ConnectGraph.h ConstraintsMath.h myOptimizer.h
g++ -Wall -g -std=c++11 -c test.cc
Mesh.o: Mesh.cc Mesh.h
g++ -Wall -g -std=c++11 -c Mesh.cc
Assembly.o: Assembly.cc Assembly.h Mate.h Model.h Mesh.h MateConnector.h ConnectGraph.h myOptimizer.h
g++ -Wall -g -std=c++11 -c Assembly.cc
Part.o: Part.cc Part.h Model.h Mesh.h MateConnector.h
g++ -Wall -g -std=c++11 -c Part.cc
Mate.o: Mate.cc Mate.h Model.h Mesh.h MateConnector.h
g++ -Wall -g -std=c++11 -c Mate.cc
assembly.pb.o: assembly.pb.cc assembly.pb.h
g++ -Wall -g -std=c++11 -c assembly.pb.cc
MateConnector.o: MateConnector.cc MateConnector.h ConstraintsMath.h Model.h
g++ -Wall -g -std=c++11 -c MateConnector.cc
ConstraintsMath.o: ConstraintsMath.cc ConstraintsMath.h
g++ -Wall -g -std=c++11 -c ConstraintsMath.cc
ConnectGraph.o: ConnectGraph.cc ConnectGraph.h Part.h MateConnector.h
g++ -Wall -g -std=c++11 -c ConnectGraph.cc
Model.o: Model.cc Model.h Mesh.h Mate.h
g++ -Wall -g -std=c++11 -c Model.cc
myOptimizer.o: myOptimizer.cc myOptimizer.h
g++ -DIL_STD -I/Applications/CPLEX_Studio129/cplex/include -I/Applications/CPLEX_Studio129/concert/include \
-Wall -g -std=c++11 -c myOptimizer.cc \
-L/Applications/CPLEX_Studio129/concert/lib/x86-64_osx/static_pic\
-L/Applications/CPLEX_Studio129/cplex/lib/x86-64_osx/static_pic \
-lcplex
clean:
rm -rf test *.o *~ *.dSYM
The error it showed is:
c++ -c -o myOptimizer.o myOptimizer.cc
myOptimizer.cc:4:10: fatal error: 'ilcplex/ilocplex.h' file not found
#include <ilcplex/ilocplex.h>
^~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [myOptimizer.o] Error 1
I'm getting errors when trying to run the HTTP server example that comes with the source of the boost library, under the path: boost_1_59_0/libs/asio/example/cpp11/http/server/.
I already ran this following commands in the boost_1_59_0 directory:
$ ./bootstrap.sh
$ sudo ./bjam install
$ sudo ./b2 install
After installing all targets, i tried to compile the main.cpp and the server.cpp with this command: g++ -std=c++0x -o main main.cpp -I "/home/user/Desktop/boost_1_59_0" -L "/home/user/Desktop/boost_1_59_0/libs/" -lboost_system.
Any suggestion on how to compile this server example?
I linked all files from the boost_1_59_0/libs/asio/example/cpp11/http/server/ folder after the main.cpp, as #Richard Hodges suggested. It still didn't work, i got errors concerning lpthread, so i added it to the compiling options. The program compiled but it failed the execution, returning an error saying that it didn't find the library libboost_system.so.1.59.0. I tried linking the folders with -L /path/to/library but it didn't work.
Solution:
My compilation command:
g++ -std=gnu++0x -o main main.cpp server.cpp connection.cpp connection_manager.cpp reply.cpp mime_types.cpp request_handler.cpp request_parser.cpp -I "/home/user/Desktop/boost_1_59_0" -lboost_system -lpthread
I solved it with this commands:
$ LD_LIBRARY_PATH="/usr/local/lib/"
$ sudo ldconfig
And then I just ran the executable and it worked!
Here's a simple makefile I just concocted that works:
all:server
CPPFLAGS+=-std=c++11 -Wall -pedantic
CPPFLAGS+=-g -O2
CPPFLAGS+=-pthread
LDFLAGS+=-lboost_system
%.o:%.cpp
$(CXX) $(CPPFLAGS) $^ -c -o $#
server:$(patsubst %.cpp,%.o,$(wildcard *.cpp))
$(CXX) $(CPPFLAGS) $^ -o $# $(LDFLAGS)
It runs make:
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.cpp -c -o connection.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection_manager.cpp -c -o connection_manager.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread main.cpp -c -o main.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread mime_types.cpp -c -o mime_types.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread reply.cpp -c -o reply.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_handler.cpp -c -o request_handler.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_parser.cpp -c -o request_parser.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread server.cpp -c -o server.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.o connection_manager.o main.o mime_types.o reply.o request_handler.o request_parser.o server.o -o server -lboost_system
And the test program runs:
$ ./server 0.0.0.0 9889 . &
$ GET http://localhost:9889/main.cpp > main.cpp.0
Check the files
$ md5sum main.cpp*
be5dc1c26b5942101a7895de6baedcee main.cpp
be5dc1c26b5942101a7895de6baedcee main.cpp.0
Don't forget to kill the server when you're done
When compiling my code I run into an issue as follows:
io.cpp:21: undefined reference to `PQconnectdb'
as well as all other instances of missing postgres function calls occurring in my code. Obviously this is a linking problem, I'm just not sure what the link issue is.
I'm compiling with the following:
mpiCC -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ decisioning_mpi.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ io.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ calculations.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ rules.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Instrument.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Backtest_Parameter_CPO.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Backtest_Trade_CPO.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Data_Bar.cpp
mpiCC -o decisioning_mpi -O2 -g -Wall -Werror -L/usr/lib -lm -lpq decisioning_mpi.o
io.o calculations.o rules.o Instrument.o Backtest_Parameter_CPO.o Backtest_Trade_CPO.o Data_Bar.o
It should be noted that this is the correct directory for libpq-fe.h and that I'm linking pq, so I'm not exactly sure why the postgres functions aren't linking correctly. I'm running Ubuntu 12.04 and installed psql (PostgreSQL) 9.1.6 from synaptic. As well I'll short circuit this, I am using #include "libpq-fe.h".
Any ideas on how I can get this linking issue resolved?
put -L/usr/lib/ -lm -lpq in the end of link command, the linker can then find the symbols
mpiCC -o decisioning_mpi -O2 -g -Wall -Werror decisioning_mpi.o io.o \
calculations.o rules.o Instrument.o Backtest_Parameter_CPO.o \
Backtest_Trade_CPO.o Data_Bar.o -L/usr/lib -lm -lpq
GCC Link Reference:
http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html