I have the following makefile in my c++ program
#All Targets
all: bin/cTrace
bin/cTrace: bin/main.o bin/agent.o bin/session.o bin/graph.o bin/tree.o
#echo 'Building target: main'
#echo 'Invoking: C++ Linker'
g++ -o bin/cTrace bin/main.o bin/agent.o bin/graph.o bin/tree.o bin/session.o
#echo 'Finished building target: main'
#echo ' '
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp
bin/agent.o: src/Agent.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/agent.o src/Agent.cpp
bin/session.o: src/Session.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/session.o src/Session.cpp
bin/graph.o: src/Graph.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/graph.o src/Graph.cpp
bin/tree.o: src/Tree.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/tree.o src/Tree.cpp
but when I write at the terminal: make I get the following message:
'makefile:7 *** missing separator stop'
I have been looking for the problem and I understand that the main cause is that the text editor may change tab to space so I check throughout all my makefile and I hadn't found any problem.
What else can cause it?
edit:
after I compile cat -e -t -v Makefile I get the following result:
spl211#spl211:~/CLionProjects/cTrace$ cat -e -t -v makefile
#All Targets^M$
all: bin/cTrace^M$
^M$
bin/cTrace: bin/main.o bin/agent.o bin/session.o bin/graph.o bin/tree.o^M$
#echo 'Building target: main'^M$
#echo 'Invoking: C++ Linker'^M$
g++ -o bin/cTrace bin/main.o bin/agent.o bin/graph.o bin/tree.o bin/session.o^M$
#echo 'Finished building target: main'^M$
#echo ' '^M$
^M$
bin/main.o: src/main.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp^M$
^M$
bin/agent.o: src/Agent.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/agent.o src/Agent.cpp^M$
^M$
bin/session.o: src/Session.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/session.o src/Session.cpp^M$
^M$
bin/graph.o: src/Graph.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/graph.o src/Graph.cpp^M$
^M$
bin/tree.o: src/Tree.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/tree.o src/Tree.cpp
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
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....
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
I am making a makefile and one of the targets is exptrtest.o how do I use g++ to create an objectfile with that name, the name of my cpp file is exprtest.cpp not exptrtest.cpp?
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exprtest.cpp
to make it more clear, this is my makefile:
all: exprtest
exprtest: exptrtest.o driver.o parser.tab.o scanner.o
g++ -Wall -g -o exprtest exptrtest.o driver.o parser.tab.o scanner.o
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exptrtest.o exprtest.cpp
driver.o: driver.cpp scanner.hpp driver.hpp
g++ -Wall -g -c driver.cpp
parser.tab.o: parser.tab.hpp parser.tab.cpp
bison parser.ypp
g++ -Wall -g -c parser.tab.cpp
scanner.o: scanner.cpp scanner.hpp
flex -t scanner.ll > scanner.cpp
g++ -Wall -g -c scanner.cpp
clean:
rm parser.tab.hpp parser.tab.cpp scanner.cpp
I'm getting the error:
"g++: error: exptrtest.o: No such file or directory
make: * [exprtest] Error 1"
Use the -o option in conjunction with -c.
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exprtest.cpp -o exptrtest.o