[英]Undefined reference error on linking - Makefile and includes configured
我有兩個源文件,main.cpp 和 functions.cpp,一個 header 歸檔 main.h,最后一個 Makefile:
主文件
#include "main.h"
int main()
{
Application game;
game.update();
game.draw();
}
函數.cpp
#include "main.h"
Application::Application()
{
window = SDL_CreateWindow("SDL GAME",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT, 0);
if(!window)
{
printf("Error: %s\n", SDL_GetError());
}
windowSurface = SDL_GetWindowSurface(window);
if(!windowSurface)
{
printf("Error: %s\n", SDL_GetError());
}
}
Application::~Application()
{
SDL_FreeSurface(windowSurface);
SDL_DestroyWindow(window);
}
void Application::update()
{
bool quit = false;
while(!quit)
{
SDL_Event e;
while(SDL_PollEvent(&e) > 0) //Event queue
{
switch(e.type)
{ //Add events here
case SDL_QUIT:
quit = true;
break;
}
}
//DRAW
Application::draw();
//STOP DRAW
SDL_UpdateWindowSurface(window);
}
}
void Application::draw()
{
SDL_UpdateWindowSurface(window);
}
主文件
#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
class Application
{
public:
Application();
~Application();
void update();
void draw();
private:
SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Event event;
};
Makefile
.PHONY = all clean
CC = g++
SRCS:= $(wildcard src/*.cpp) # Succesfully grabs all source files
BINS := $(SRCS:%.cpp=%)
LINKERFLAG = -lSDL2 -Isrc
all: ${BINS}
%: %.cpp
${CC} ${LINKERFLAG} $< -o $@.o
%.o: %.cpp
${CC} -o $<.o
clean:
rm -rvf *.o ${BINS}
錯誤
g++ -lSDL2 -Isrc src/main.cpp -o src/main.o /usr/lib64/gcc/x86_64-suse-linux/10/../../../../x86_64-suse-linux/ bin/ld: /tmp/ccp2ZmwE.o: in function
main': main.cpp:(.text+0x11): undefined reference to
Application::Application()' /usr/lib64/gcc/x86_64-suse-linux/ 10/../../../../x86_64-suse-linux/bin/ld: main.cpp:(.text+0x1d): 未定義引用Application::update()' /usr/lib64/gcc/x86_64-suse-linux/10/../../../../x86_64-suse-linux/bin/ld: main.cpp:(.text+0x29): undefined reference to
Application::draw ()' /usr/lib64/gcc/x86_64-suse-linux/10/../../../../x86_64-suse-linux/bin/ld: main.cpp:(.text+0x35) : 未定義引用Application::~Application()' /usr/lib64/gcc/x86_64-suse-linux/10/../../../../x86_64-suse-linux/bin/ld: main.cpp:(.text+0x4b): undefined reference to
我試過的:使用 -c 編譯所有然后再次運行而不鏈接,沒有成功。 我來到 c++ 並從 python 背景制作,所以它對我來說相當新。 我認為正在發生的是它試圖在編譯functions.cpp之前鏈接main.cpp,但我不知道如何解決這個問題。
假設您有一個獨立的源文件foo.cpp
。
您可以一步構建可執行文件foo
:
g++ foo.cpp -o foo
或者您可以構建 object 文件,然后從中構建可執行文件:
g++ -c foo.cpp -o foo.o
g++ foo.o -o foo
現在假設您有兩個源文件main.cpp
和functions.cpp
。 main.cpp
中的代碼調用了functions.cpp
中定義的 function 。 如果您不小心嘗試僅從一個文件構建可執行文件:
g++ main.cpp -o app
編譯器會抱怨您的代碼調用了沒有定義的 function。 這就是您的 makefile 在嘗試構建main
時所做的事情:
%: %.cpp
${CC} ${LINKERFLAG} $< -o $@.o
在這種情況下,通常的方法是先構建 object 文件,然后鏈接它們:
g++ -c main.cpp -o main.o
g++ -c functions.cpp -o functions.o
g++ main.o functions.o -o name_of_executable
您可以使用如下所示的 makefile 來做到這一點:
$(EXEC_NAME): main.o functions.o
${CC} ${LINKERFLAG} $^ -o $@
%.o: %.cpp
${CC} -c $< -o $@
還有更多的改進(你甚至不必編寫第二條規則,Make 已經知道了),但這應該足以讓你開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.