我正试图编译多个.cpp文件,使用一个带有以下内容的Makefile来编译 使 . I have a .cpp file containing the main function, in the other .cpp file a basic class (for every doubt I'll put all the code i'm using down here). Example of names used to 使 it compile or to 使 it not compile:
-它的工作原理是将它们命名为 "prova.cpp"(包含主函数)和 "pa.cpp"(包含类)(在Makefile完成的命令下面)。
gioele@GioPC-U:~/Dev/Cpp/Exercises/666prova$ make
g++ -g -Wall -c src/prova.cpp -o obj/prova.o
g++ -g -Wall -c src/pa.cpp -o obj/pa.o
g++ -g -Wall obj/prova.o -o bin/provaBin
-it doesn't通过将它们命名为 "ciao.cpp"(包含主函数)和 "pa.cpp"(包含类)来工作。
gioele@GioPC-U:~/Dev/Cpp/Exercises/666prova$ make
g++ -g -Wall -c src/pa.cpp -o obj/pa.o
g++ -g -Wall -c src/ciao.cpp -o obj/ciao.o
g++ -g -Wall obj/pa.o -o bin/provaBin
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:15: bin/provaBin] Errore 1
我认为问题出在Makefile里面的文件的顺序上。当它不工作时,是因为试图从没有主函数的.o中获取二进制文件。不知道如何解决。
有主方法的文件。
#include <iostream>
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
该类文件。
class Class {
public:
int x;
int y;
The Makefile (still learning to work with Makefiles, probably the problem is here, any advice to 使 a Makefile of this kind better is appreciated, or if I am using something not properly):
CC=g++
CFLAGS=-g -Wall
OBJ=obj
SRC=src
BINDIR=bin
BIN=$(BINDIR)/provaBin
SRCS=$(wildcard $(SRC)/*.cpp)
OBJS=$(patsubst $(SRC)/%.cpp, $(OBJ)/%.o, $(SRCS))
all: $(BIN)
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $< -o $@
$(OBJ)/%.o: $(SRC)/%.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
$(RM) $(BINDIR)/* $(OBJ)/*
预先感谢你。