makefile 中未定义的引用

发布于 2024-12-17 19:42:25 字数 1457 浏览 4 评论 0原文

好吧,我读了大约 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) 

我一直告诉我它有未定义的引用。我缺少什么?

makefile 输出什么

/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。

Ok, ive read about 10 tutorials, but i keep getting errors all the time, i have 5 files, main.cpp
class.cpp, class.h and functions.cpp and functions.h. All of those use functions from different objects meaning that functions in functions.cpp uses objects from classes.cpp.

My makefile looks as follows

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) 

I keeps telling me that it has undefined references. What am i missing?

What makefile outputs

/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

The files compile great when i had them in Visual C++, so it has to be my makefile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-12-24 19:42:25

你确实在做一些奇怪的事情。您应该编译(-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),因为此时不会发生链接。

You are indeed doing something strange. What you should is to compile (-c) the object files and then link them together. This would look like this:

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

While you are doing this, it is even better if you compiled main.o separately also. Therefore:

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)

Also note that I removed the $(LIBS) when using -c because linking doesn't happen then.

自由如风 2024-12-24 19:42:25

您正在尝试将 .o 文件链接到可执行文件。将 -c 添加到编译标志中,以便它仅为您的目标文件进行编译。

像这样将其作为第一个选项

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

You are trying to link your .o files into executables. Add a -c to the compile flags so it compiles only for you object files.

Make it the first option like this

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH)
    $(CC) -c -o $@ $(SDLF) $(LIBS)
南冥有猫 2024-12-24 19:42:25

你有一个错字。您正在使用 $(CLASSESH) 但声明了 CLASSH

You have a typo. You're using $(CLASSESH) but declared CLASSH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文