cat*_*eia -2 c c++ gcc makefile g++
我有一个g++ -std=c++17 -Wall -Og -g -o main *.cc完美编译的小代码.现在我想要有一个makefile,到目前为止我得到了这个:
CC = g++
CFLAGS = -Wall -std=c++17 -Og -g
DEPS = random_tools.h
OBJ = main.o random_tools.o
%.o: %.c $(DEPS)
$(CC) $(CFLAGS) -o $@ $<
main: $(OBJ)
gcc $(CFLAGS) -o $@ $^
Run Code Online (Sandbox Code Playgroud)
然而,当我跑make它崩溃时,告诉我
g++ -c -o main.o main.cc
g++ -c -o random_tools.o random_tools.cc
gcc -Wall -o main main.o random_tools.o
main.o: In function `main':
main.cc:(.text+0x1d): undefined reference to `std::cout'
main.cc:(.text+0x22): undefined reference to `std::ostream::operator<<(int)'
main.cc:(.text+0x27): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main.cc:(.text+0x2f): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cc:(.text+0x5d): undefined reference to `std::ios_base::Init::Init()'
main.cc:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make: *** [makefile:10: main] Error 1
Run Code Online (Sandbox Code Playgroud)
我真正没有得到的是为什么它使用gcc而不是g++- 我告诉它使用g++.有人可以了解这里发生的事情,以及我如何make做我说的话?谢谢.
这个规则
main: $(OBJ)
gcc $(CFLAGS) -o $@ $^
Run Code Online (Sandbox Code Playgroud)
将"gcc"硬连接到其中.将其更改为$(CC),它将按预期运行.
也就是说,这样写Makefile会更好:
CXX = g++
CXXFLAGS = -Wall -std=c++17 -Og -g
DEPS = random_tools.h
OBJ = main.o random_tools.o
# Default goal
main: $(OBJ)
$(LINK.cc) $^ -o $@ $(LDLIBS)
# Header dependencies
$(OBJ): $(DEPS)
Run Code Online (Sandbox Code Playgroud)
这使用内置的Make约定,因此以后扩展会更容易.(我没有足够的空间来解释所有内置的Make约定.我建议你阅读GNU Make手册封面.)
(请注意,这不是你所期望的,如果你真的叫你的源文件做的main.c和random_tools.c,而不是main.cc和random_tools.cc,但你应该使用.cc的C++源文件反正.)
| 归档时间: |
|
| 查看次数: |
903 次 |
| 最近记录: |