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.
Related
I'm having some troubles with the GNU Scientific Lbrary (GSL).
After having intalled it, I tested it with the example here and it works when compiling with:
gcc -Wall -c main.cpp ; gcc -L/usr/local/lib main.o -lgsl -lgslcblas -lm -libtemt
Next, I tried to compile that with no change in the source code nor the compiling command. But here is the problem, when I try to run the result, I get :
./a.out: symbol lookup error: ./a.out: undefined symbol: gsl_multifit nlinear_trs_lmaccel
I tried to comment it out, but it's making other issues. I found that variable in the fulle doc, but I can't find where it's defined.
As the first example is working, I think GSL was installed successfully, and as the same variableis used in different codes, I think it's a global variable, defined in th library.
Does anyone have an idea why I can't access it?
Thank's a lot!
Well, after a lot of tears (no, it's a joke) I found a sort of answer:
I copy and paste libgsl.a and libgslcblas.a in my directory, i wrote that makefile:
CC=gcc
CXXFLAGS=-W -Wall -ansi -pedantic
LIBS=libgsl.a libgslcblas.a -lm
main:main.o
$(CC) -o main main.o $(LDFLAGS) $(LIBS)
main.o: main.cpp
$(CC) -o main.o -c main.cpp $(CXXFLAGS)
I'm happy with that answer even if there is 2 weaknesses:
1) I don't know how to use shared library (at this moment)
2) I don't know how to make it by command line
Hope it will help someone
I am trying to compile a game using g++ and the SFML library. It works fine on Linux, but when I try to compile on Windows, the cmd floods me with error messages that all start with "player.o:player.cpp:(.text+xxxxxx):undefined reference to _imp___..."
My makefile looks like this:
all:
main.o player.o simplefunctions.o
g++ -std=c++11 main.o player.o simplefunctions.o -o climb -L C:/SFML-2.3.1/lib -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
main.o: main.cpp
g++ -std=c++11 -c main.cpp -I C:/SFML-2.3.1/include
player.o: player.cpp
g++ -std=c++11 -c player.cpp -I C:/SFML-2.3.1/include
simplefunctions.o: simplefunctions.cpp
g++ -std=c++11 -c simplefunctions.cpp -I C:/SFML-2.3.1/include
.cpp -I C:/SFML-2.3.1/include
The only difference between this and my makefile I use for the same program on ubuntu is the -I and -L tags to include the library, which I don't need on linux since it gets included automatically. All the appropriate header files are also included in the appropriate cpp's.
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?
I can't get this makefile error to go away. I have tried every syntax on earth and I can't figure out what is wrong.
My makefile reads:
huffmanCodeGenerator: main.o
g++ -o huffmanCodeGenerator main.o
main.o: main.cpp ReadFile.h HuffmanCode.h ConstructTree.h WriteFile.h
g++ -c main.cpp
ReadFile.o: ReadFile.cpp ReadFile.h
g++ -c ReadFile.cpp
HuffmanCode.o: HuffmanCode.cpp HuffmanCode.h
g++ -c HuffmanCode.cpp
ConstructTree.o: ConstructTree.cpp ConstructTree.h
g++ -c ConstructTree.cpp
WriteFile.o: WriteFile.cpp WriteFile.h
g++ -c WriteFile.cpp
clean:
&nbps rm *.o huffmanCodeGenerator
I have all of those files in my directory and I keep getting
error 2 - the system cannot find the file specified.
Here is a screenshot of my directory and error:
Any suggestions on how to fix this? I don't care how pretty or how correct the syntax is. As long as it makes.
First problem I see is that your first two lines are incorrect. Linker needs all the object files and not just main.o to link them together.
Secondly, are you sure your Windows knows the path to g++? What happens if you try g++ --help in cmd, for example? (Not sure about this though, I run it like that in Unix).
Thirdly, this looks similar to your problem: Internal Builder: Cannot run program "g++": The system cannot find the file specified
This link: leads to a conclusion that you need to:
1) have g++.exe in your current folder or
2) have it on your PATH
I am running into problems when linking SDL_image in gcc 3 on cygwin under Windows 7.
I receive the following error:
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lSDL_image
My makefile appears as this:
all: rabbit
rabbit: main.o rabbit.o renderer.o
g++ -o rabbit main.o rabbit.o renderer.o -lSDLmain -lSDL -lSDL_image
main.o: main.cpp rabbit.h
g++ -c main.cpp
rabbit.o: rabbit.cpp rabbit.h gameobject.h
g++ -c rabbit.cpp
renderer.o: renderer.cpp renderer.h
g++ -c renderer.cpp
clean:
rm -rf *o rabbit
I store SDL_image.dll, SDL_image.lib, jpeg.dll, libpng12-0.dll, libtiff-3.dll, and zlib1.dll in the directory with my executable. SDL_image.h is also in the correct location.
Please help, this has been at fault for a few days now!
SDL_image is not part of the core SDL distribution. It must be installed separately.
Check if you have /usr/local/lib/libSDL_image.a or the shared library installed.