【问题标题】:Can't figure out how to properly format Makefile using SDL2. Undefined reference无法弄清楚如何使用 SDL2 正确格式化 Makefile。未定义参考
【发布时间】:2020-09-15 04:34:44
【问题描述】:

生成文件:

OBJS = Instantiation
Exec_NAME = Test.exe
CC = g++ #Compiler name
COMPILER_FLAGS = -c -g -Wall -std=c++11
#Issue exists somewhere in these next 3 lines, but what, how, why :( ?
INC = -I/SDLDEPS/include
LIB = -L/SDLDEPS/lib
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 #just saw this a lot, not sure what the mingw32 is.

#inclusion of $(INC) $(LIB) $(LINKER_FLAGS) not right
all: $(Exec_NAME)
$(Exec_NAME): $(OBJS).o
    $(CC) -o $(Exec_NAME) $(OBJS).o
Instantiation.o: $(OBJS).cpp
    $(CC) $(COMPILER_FLAGS) $(INC) $(LIB) $(LINKER_FLAGS) $(OBJS).cpp

clean:
        rm -f $(Exec_NAME) $(OBJS).o
rebuild:
        make clean
        make

//Main.cpp
#include <iostream>
#include "Window.h"
//#include "SDLDeps/include/SDL.h"
#include "SDLDeps/include/SDL.h"
int main(int argc, char** argv){
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
        std::cerr << "SDL Failed to initalize\n";
    else
        std::cout << "Great Success";
    Window window("TEST WINDOW");
    while(!window.isClosed()){
        window.pollEvents();
        window.Clear();
    }
    //std::string string = "";
    //std::cout << "HELLO PERSON What is your name?\n";
    //std::cin >> string;
    //std::cout << string << "!\n";
    return 0;
}

//Window.h I don't think Window.cpp is necessary as the issue is linking, I think.
#ifndef WINDOW_H
#define WINDOW_H
#include <string>
#include "SDLDeps/include/SDL.h"
//#include "SDL.h"
#include <iostream>

class Window {
private:
    std::string title;
    int width, height;

    bool closed;    
    
    bool init();

    SDL_Window* window;
    SDL_Renderer* renderer;
public:
    Window(const std::string&, int = 800, int = 600);
    ~Window();
    
    void pollEvents();
    void Clear() const;
    inline bool isClosed() { return closed; }
};
#endif // !WINDOW_H

//    Instantiation.cpp- just a file that includes files that I am compiling:
        #include "Window.cpp"
        #include "Main.cpp"

我对使用 makefile 进行链接和“高级”操作相当陌生。但是几个小时 谷歌搜索我不知道为什么我不断得到: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:13:未定义对SDL_DestroyWindow' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:14: undefined reference to SDL_DestroyRenderer'的引用 /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:15: 未定义引用SDL_Quit' /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:19: undefined reference to SDL_Init' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:23: 未定义引用SDL_CreateWindow' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:28: undefined reference to SDL_CreateRenderer' /usr/bin/ld: Instantiation.o: 在函数Window::pollEvents()': /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:38: undefined reference to SDL_PollEvent' /usr/bin/ld: Instantiation.o: 在函数Window::Clear() const': /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:72: undefined reference to SDL_SetRenderDrawColor' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:73: 未定义引用SDL_RenderClear' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:81: undefined reference to SDL_SetRenderDrawColor' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:82: 未定义引用SDL_RenderFillRect' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:88: undefined reference to SDL_RenderPresent'

所有 SDL 的东西都是未定义的,但我没有收到错误说 SDL.h 是未定义的,所以它要么与库有关,要么与其他东西有关 文件路径是: TestSDL-基本目录 TestSDL\SDLDeps- 包括 2 个文件夹 include 和 lib 包括 - 包括所有 .h 文件 lib- 包括 SDL2.dll、SDL2、SDL2Main、SDL2test 如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

    标签: c++ ubuntu makefile sdl-2


    【解决方案1】:

    对于不同的组件,有很多不同的SDL2 库。我使用pkg-config 来获取编译和链接器标志,有点像这样:

    SDL2_INCS := $(shell pkg-config sdl2 --cflags) \
        $(shell pkg-config SDL2_image --cflags) \
        $(shell pkg-config SDL2_ttf --cflags) \
        $(shell pkg-config SDL2_net --cflags) \
        $(shell pkg-config SDL2_mixer --cflags)
    
    SDL2_LIBS := $(shell pkg-config sdl2 --libs) \
        $(shell pkg-config SDL2_image --libs) \
        $(shell pkg-config SDL2_ttf --libs) \
        $(shell pkg-config SDL2_net --libs) \
        $(shell pkg-config SDL2_mixer --libs)
    
    Instantiation.o: $(OBJS).cpp
        $(CC) $(COMPILER_FLAGS) $(SDL2_INCS) -o Instantiation.o $(SDL2_LIBS) $(OBJS).cpp
    

    【讨论】:

    • 您应该能够将多个库传递给单个 pkg-config 调用。
    猜你喜欢
    • 2011-02-18
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2019-03-12
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多