Find library corresponding to a file - c++

I am working on a c++ project. It's output are so files. produced by calling a make file.
For my test cases(using cppunit) I want a output in the form of an executable.
I tried the following command g++ -o output File1.o -lcppunit. It gives me the output file with proper results.
But if I include some file from the project(File2) in the test case program (whose .o file I have), it gives me various unresolved symbols. I get that error if in the previous command I hadn't put the -lcppunit. So I understand, that corresponding to the symbol I need it's library. But I dont know what library to include.
Please help me with that. Any help would be appreciated.
These are various commands that are run from the makefile.
To get the .o file:
g++ -Wall -g -fPIC -D_REENTRANT -DRW_MULTI_THREAD -DRW_NO_STL -DRW_COMPILE_INSTANTIATE -DRW_BCB_NEW_IOSTREAMS -DRW_RWV2X_STDLIB -DRW_NO_XDR -D_GLIBCPP_DEPRECATED -DPM_32BIT -DFD_SETSIZE=4096 -DUNIX -DLINUX -DCHECK_BUFPOOL_INTEGRITY -DOPB_DEBUG -DRWDEBUG -DDEBUG -DINFA_PREFIX -I. -IAllIncludePaths -c File.cpp -o File.o
To get the so files:
g++ -shared -ldl -lpthread -Wl,--hash-style=both File.o File1.o File2.o File3.o
-o /xyz.so
-LAlllinkPaths -llibraries
also what should my all: in makefile have?

Related

Makefile with pthread won't compile properly with multiple versions

For a project, I have to create a simple makefile for the source code which includes pthreads and command line arguments (if those matter to include).
The first version of the makefile that didn't work was this:
mr: mr.o
g++ -std=c++11 -pthread mr.o -o mr
mr.o: mapred.cc
g++ -std=c++11 -pthread -c mapred.cc
clean:
rm *.o mr
and I got the following error that the object file did not exist?
So then I decided to flip the two first statements around:
mr.o: mapred.cc
g++ -std=c++11 -pthread -c mapred.cc
mr: mr.o
g++ -std=c++11 -pthread mr.o -o mr
clean:
rm *.o mr
and it compiles, sort of? All I get in the terminal is:
g++ -std=c++11 -pthread -c mapred.cc
and nothing else. When I look at what files were created, all I see is a new file mapred.o created but no executable. So no errors but not fully completed. If you guys have any tips to help me out that would be very appreciated. Thank you!
If you don't specify an explicit output name with the -o option, then the compiler will name object file the same as the source file but with an .o suffix.
In your case, the command
g++ -std=c++11 -pthread -c mapred.cc
will create an object file named mapread.o.
Either use mapread.o for your target names and when linking, or use the -o option:
g++ -std=c++11 -pthread -c mapred.cc -o mr.o
As for your second problem, unless you specify an explicit target when invoking make, it will only use the first target and nothing else.

How to link obj files without crt?

how to can i achieve linking 2 .o file without CRT.
Compiling .c files:
gcc -ffreestanding -c file.c -o file.o
Linking:
gcc file1.o file2.o -o f.o
Flags -nostartfiles and -nostdlib don't help me.
I just compile 2 .c file and i want to link their in 1 object file
ld -r -o f.o file1.o file2.o

G++ stops working when used in makefile

I'm currently trying to use a makefile to automate my build process which I am still new to. When I run the g++ command:
g++ -I includes -L lib -lSDL2_test -lSDL2 -lSDL2main main.o -o main.exe
from the following makefile:
main: main.o
g++ -I includes -L lib -lSDL2_test -lSDL2 -lSDL2main main.o -o main.exe
main.o: main.cpp
g++ -c main.cpp
I get clang: error: linker command failed with exit code 1
However when running the same g++ command from the same directory as the makefile everything compiles correctly and I don't get the error. So how do I fix this?
EDIT:
I've done some testing and found out the problem with the makefile is that the -I includes g++ flag is not enabling the main.o file to find SDL.H, hopefully that helps to narrow down the problem.
#G.M's comment isn't unrelated - it's the correct answer to the problem. The include files are needed during the compile stage (the main.o: main.cpp rule), NOT during the link stage (i.e. the main: main.o rule.)
Move the -I includes flag to the second rule, and try again.

C++ - Makefile using g++

I have made a Makefile for my CMSC 202 course project, 'Blackjack'. It does everything I need it to and it works perfectly. You may be asking why I posted here then, this is because I have no idea how it works and I didn't use any other resources but myself to create it.
Here is my Makefile code.
# Object files to either reference or create
OBJECTS = Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
# The executable file that will be created at the end
EXEC = Proj2.out
# The flags to use for compilation
FLAGS = -Wall
# The code compiler to use for compilation
CC = g++
# Perform action on all object files (May or may not exist)
all: $(OBJECTS)
$(CC) $(FLAGS) -o $(EXEC) $(OBJECTS)
Here is the terminal output when I call make in the terminal.
g++ -c -o Proj2.o Proj2.cpp
g++ -c -o Blackjack.o Blackjack.cpp
g++ -c -o Deck.o Deck.cpp
g++ -c -o Card.o Card.cpp
g++ -c -o Hand.o Hand.cpp
g++ -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
Can anyone tell me how the .o files are being compiled? It does not look like they are being prompted to be compiled with that g++ -c -o $.o $.cpp command anywhere in the Makefile. Nor did I state to use any .cpp files.
Thank you in advance for your help.
Edit
Thanks to all your great help, this is now the terminal output I receive when using make.
g++ -Wall -c -o Proj2.o Proj2.cpp
g++ -Wall -c -o Blackjack.o Blackjack.cpp
g++ -Wall -c -o Deck.o Deck.cpp
g++ -Wall -c -o Card.o Card.cpp
g++ -Wall -c -o Hand.o Hand.cpp
g++ -Wall -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
Thank you so much to all of you who have contributed.
Make has a set of implicit rules (see here for a reference). For instance
Compiling C++ programs
`n.o' is made automatically from `n.cc' or `n.C' with a command of the form
`$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'.
Most make's will also use this rule for .cpp files.
When make sees there's a x.o requirement for one of your targets, it will try to see if it can generate x.o using implicit rules, and in your case find it can do it starting from a .cpp file.
This Makefile uses implicit rules which are a great way to reduce duplication.
By default the first target will be built, here all. It depends on a number
of object files listed in a variable $OBJECTS, e.g. Proj2.o who's
dependencies aren't listed in the Makefile. Now if make sees an input file in the current directory
with a matching name, e.g. Proj2.cpp it will try
to build Proj2.o from it (there are other implicit rules for sources in
other languages). Proj2.o would then be built by default with the command
$(CXX) $(CXXFLAGS) -c -o Proj2.o
where $(CXX) the name of the C++ compiler (g++ in your case).
The explicit build step for all assembles all the object files into the
target executable.
Looking at above build command you'll notice a small problem in your Makefile. Since the flags to the C++ compiler are given in a variable FLAGS and not the standard CXXFLAGS no warnings will be emitted when building the object files. Using the standard name would fix this (you do want warnings, maybe even more than -Wall gives you).

GLEW based program does not compile

I am trying to use GLEW in a program I'm creating, but my compiler will not compile it, instead it throws a ton of errors at this line gcc -g -c glew.c -o glew.o. This is my Makefile:
MY_LIBS =
glewex: glew.o main.o glew.h
g++ main.o glew.o glew.h -o glewex $(MY_LIBS)
glew.o: glew.c
gcc -g -c glew.c -o glew.o
main.o: main.cpp
g++ -g -c main.cpp -o main.o
It simply outputs hundreds of errors that look like this:
__glewActiveTexture redeclared without dllimport attribute: previous import ignored [ -Wattributes ]
Try this:
gcc -g -DGLEW_STATIC -c glew.c -o glew.o
That should prevent DLL import/export decorations from getting added to the declarations.
You don't want to add the library source files to the compiler input of your project. You should add the library to the list of linker inputs; either statically (libglew.a) or dynamic (-lglew).
I.e. either
gcc -o … -lglew
or
gcc -o … libglew.a
When linking GLEW statically you must add -DGLEW_STATIC to the compiler options generating the compilation units (.o files)