c++ - makefile 中 undefined reference

标签 c++ linux makefile g++

好的,我已经阅读了大约 10 个教程,但我总是出错,我有 5 个文件,main.cpp class.cpp、class.h 和 functions.cpp 以及 functions.h。所有这些都使用来自不同对象的函数,这意味着 functions.cpp 中的函数使用来自 classes.cpp 的对象。

我的 makefile 如下所示

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
SRC = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o $(SRC)
    $(CC) -o $@ $(SRC) $(LIBS)

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ $(SDLF) $(LIBS)

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ $(CLASS) $(LIBS) 

我一直告诉我它有 undefined reference 。我错过了什么?

什么生成文件输出

/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccJG6yQA.o: In function `DrawEnemies(SDL_Surface*)':
SDLfunctions.cpp:(.text+0x3a7): undefined reference to `Enemy::sprite'
/tmp/ccJG6yQA.o: In function `rysujpociski(int, SDL_Surface*, SDL_Surface*, 
std::vector<AllyBullet, std::allocator<AllyBullet> >&, double)':
SDLfunctions.cpp:(.text+0x141f): undefined reference to `AllyBullet::sprite'
/tmp/ccJG6yQA.o: In function `global constructors keyed to width':
SDLfunctions.cpp:(.text+0x14a7): undefined reference to `Enemy::Enemy()'
collect2: ld returned 1 exit status
make: *** [SDLfunctions.o] Error 1

当我在 Visual C++ 中使用这些文件时,这些文件编译得很好,所以它必须是我的 makefile。

最佳答案

你确实在做一些奇怪的事情。您应该编译 (-c) 目标文件,然后将它们链接在一起。这看起来像这样:

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
SRC = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o $(SRC)
    $(CC) -o $@ $(SRC) SDLfunctions.o Classes.o $(LIBS)  # you forgot to link
                                                         # the object files

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ -c $(SDLF)        # -c added to compile, not link

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ -c $(CLASS)       # -c added to compile, not link

在执行此操作时,如果您也单独编译 main.o 会更好。因此:

CC = g++ -O2 -I./sdl/include -L.
LIBS = -lm -lSDL -lpthread -ldl
MAIN = main.cpp
SDLF = SDLfunctions.cpp
CLASS = classes.cpp
CLASSH = classes.h
SDLFH = SDLfunctions.h

all: main

main: SDLfunctions.o Classes.o main.o
    $(CC) -o $@ SDLfunctions.o Classes.o main.o $(LIBS)

main.o: $(SDLFH) $(MAIN) $(CLASSH)
    $(CC) -o $@ -c $(MAIN)

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -o $@ -c $(SDLF)

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH)
    $(CC) -o $@ -c $(CLASS)

另请注意,我在使用 -c 时删除了 $(LIBS),因为那时不会发生链接。

关于c++ - makefile 中 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273881/

相关文章:

c++ - 在 C++ 中实现二维区间搜索的最佳方法是什么?

c - 为启动时运行的 linux 内核添加代码

c - 我不知道这个 makefile 有什么问题?

c++ - 如何让 yacc/bison 和 lex/flex 暂停文件扫描?

c++ - 来自 getaddrinfo 和 inet_ntop 的虚假 IP 地址

c++ - 引用和绑定(bind)对象

c++ - 使用带有 linux 的 std::filesystem 是坏的还是文件系统树无法访问?

linux - createrepo 使用 --split 选项进行多 CD 分发

makefile - GNU make 总是将非文件目标视为正在重制(可能是错误?)

makefile - 检查 makefile 中的 virtualenv 激活情况