gpt4 book ai didi

c++ - 无法弄清楚如何使用 SDL2 正确格式化 Makefile。未定义引用

转载 作者:行者123 更新时间:2023-12-02 10:10:14 28 4
gpt4 key购买 nike

生成文件:

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: undefined reference SDL_Quit' /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:19: undefined reference to SDL_初始化'
/usr/bin/ld:/mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:23: undefined reference SDL_CreateWindow' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:28: undefined reference to SDL_CreateRenderer'
/usr/bin/ld: 实例化.o: 在函数 Window::pollEvents()': /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:38: undefined reference to SDL_PollEvent'
/usr/bin/ld: 实例化.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: undefined reference 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: undefined reference 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
如果有人可以提供帮助,我将不胜感激。

最佳答案

有很多不同的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

关于c++ - 无法弄清楚如何使用 SDL2 正确格式化 Makefile。未定义引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63895149/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com