gpt4 book ai didi

c++ - 加速 C++ : compiling the programs in chapter 4

转载 作者:行者123 更新时间:2023-11-30 03:56:18 25 4
gpt4 key购买 nike

我写了一个Makefile来编译文件并输出文件“main”,我最终将通过“./main”执行。下面是我的 Makefile:

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: Student_info median grade main

main: main.cpp
$(CXX) $(CXXFLAGS) main.cpp -o main grade.o median.o Student_info.o

grade: grade.h grade.cpp
$(CXX) $(CXXFLAGS) grade.cpp -o grade.o

median: median.h median.cpp
$(CXX) $(CXXFLAGS) median.cpp -o median.o

Student_info: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) Student_info.cpp -o Student_info.o

clean:
rm -f *.o
rm -f main

然而,它说

clang++ -g -std=c++11 -Wall -pedantic Student_info.cpp -o Student_info.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Student_info] Error 1

我的 Makefile 有什么问题?

最佳答案

Student_info: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) Student_info.cpp -o Student_info.o

看起来您希望将其编译为目标文件 - 而无需链接。为此,您需要传递 -c。此外,目标的名称应为 Student_info.o:

Student_info.o: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) -c Student_info.cpp -o Student_info.o

grademedian 也是如此。

main 不仅应该依赖于 main.cpp,还应该依赖于你与之链接的目标文件:

main: main.cpp Student_info.o grade.o median.o
$(CXX) $(CXXFLAGS) main.cpp -o main grade.o median.o Student_info.o

关于c++ - 加速 C++ : compiling the programs in chapter 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com