So this is my first time making a make file so its really basic. I have 2 cpp files (functions.cpp and main.cpp) and 2 header files (structDeclaration.h and Prototypes.h). It needs to be able to compile my program but if only one file changes then it shouldn't recompile the entire thing.
heres my error:
g++ -c gradebook main.o Functions.o -I.
g++: error: gradebook: No such file or directory
make: *** [gradebook] Error 1
and heres my makefile:
CC = g++
gradebook: main.o Functions.o
g++ -c gradebook main.o Functions.o -I.
main.o: main.cpp Prototypes.h structDeclaration.h Prototypes.h
g++ -c main.cpp
Functions.o: Functions.cpp structDeclaration.h
g++ -c Functions.cpp
the commands have to be valid commands. I think you mean
g++ -o gradebook main.o Functions.o
If in doubt just try typing the command you are asking make to run for you, there is no magic involved here
Related
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.
I am trying to include a sqlite3 in my cpp project but while compilation it gives below error:
g++ -c -std=c++11 -g src/Main.cpp -I"C:/Mycode/src/DB"
-L"C:/Mycode/src/DB" -lsqlite3
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32
-L"C:/Mycode/src/DB" -lsqlite3
C:/Mycode/src/DB/sqlite3.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
make: ***[Mycode.exe] Error 1
I feel during final linking time it gives error.
I am using my own make file for compilation, below is the make command I am using:
DB_DIR="C:/Mycode/src/DB"
clean:
rm Main.o Main.exe
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32 -L${DB_DIR} -lsqlite3
Main.o: src/Main.cpp
g++ -c -std=c++11 -g src/Main.cpp -I${DB_DIR} -lsqlite3
Data.o: src/Data.cpp
g++ -c -std=c++11 -g src/Data.cpp
SqliteApi.o: src/SqliteApi.cpp
g++ -c -std=c++11 -g src/SqliteApi.cpp
I have googled but I couldn't find any solution or suggestion for this error.
Any help will be much appreciated.
The recommended way of using the SQLite library is to compile the amalgamation source file directly into your program:
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o sqlite3.o -lws2_32
sqlite3.o: src/sqlite3.c
gcc -c ... src/sqlite3.c
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 had an error unexpected during my program compilation.
(I must use standard 2011 to std::regex.)
In my MinGw cmd, I go to the folder in which my programme is, and write:
g++ -Wall -c -std=c++0x main.cpp
here every thing is ok, but after when I write:
g++ -Wall -o -std=c++0x main.exe main.o
it tell me :
g++.exe: error: main.exe: No such file or directory
WHY ?
Because you're writing the switches in the wrong order. -o requires that the destination filename be after it.
Here, you've told GCC to link main.exe and main.o into the executable -std=c++0x, and main.exe doesn't even exist yet.
g++ -Wall -o -std=c++0x main.exe main.o
# ^^^^^^^^^^^^^ ????????
Instead, tell it to link main.o into the executable main.exe, in C++0x mode:
g++ -Wall -std=c++0x -o main.exe main.o
# ^^^^^^^^^^^
I've just learn something about makefile and here is my first question for it.I have main.cpp hello.cpp factorial.cpp and functions.h files
all: hello
hello: main.o factorial.o hello.o
g++ main.o factorial.o hello.o -o hello
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
hello.o: hello.cpp
g++ -c hello.cpp
clean:
rm -rf *o hello
In the code above, why files have an extention .o ? shouldnt be .cpp or what is the differences between using .cpp and .o
Building a C++ program is a two-stage process. First, you compile each .cpp file into a .o object file. Compiling converts the source code into machine code but doesn't resolve function calls from other source files (since they haven't been compiled yet).
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
hello.o: hello.cpp
g++ -c hello.cpp
Second, you link the object files together to create an executable. Linking resolves the external references in each object file.
hello: main.o factorial.o hello.o
g++ main.o factorial.o hello.o -o hello
By the way, there is a typo in the clean target. *o should be *.o.
clean:
rm -rf *.o hello
.o denote "object-files", these are files compiled from source but not yet linked into an executable or a library.
In your make-file, i.e.
main.o : main.cpp
says that main.o will be created from main.cpp using g++ -c main.cpp.
Eventually, all files with .o will create the executable hello as stated in
hello: main.o factorial.o hello.o
g++ main.o factorial.o hello.o -o hello