我是一个 C 语言编程的初学者,为了激励自己花更多时间用 C 语言编程,我想尝试游戏编程。环顾四周,发现 Raylib 是一个 C/C++ 游戏框架。
但是,我可以找到的在线文档和所有教程都使用安装程序或 IDE 包或 GIT 来安装 Raylib。这不是我想要的方式。我想“简单地”在我的 C 项目中包含 Raylib,仅此而已,但我无法让它以这种方式工作,我在 macOS 12.1 上遇到链接器错误。
我只使用纯文本编辑器和终端,没有花哨的 IDE。我从 itch.io 下载了 Raylib,并将“include”和“lib”文件夹复制到我的项目文件夹中。最后,我在我的项目文件夹中创建了“main.c”。
main.c 的内容如下所示:
#include "include/raylib.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
当我尝试使用“make main.c”编译它时,出现以下链接错误:cc main.c -o main
Undefined symbols for architecture x86_64:
"_BeginDrawing", referenced from:
_main in raylib-7a2ee2.o
"_ClearBackground", referenced from:
_main in raylib-7a2ee2.o
"_CloseWindow", referenced from:
_main in raylib-7a2ee2.o
"_DrawText", referenced from:
_main in raylib-7a2ee2.o
"_EndDrawing", referenced from:
_main in raylib-7a2ee2.o
"_InitWindow", referenced from:
_main in raylib-7a2ee2.o
"_SetTargetFPS", referenced from:
_main in raylib-7a2ee2.o
"_WindowShouldClose", referenced from:
_main in raylib-7a2ee2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [raylib] Error 1
文件夹结构如下所示:
MyProject
-include
--raygui.h
--raylib.h
--rlgl.h
-lib
--libraylib4.0.0.dylib
--libraylib400.dylib
--libraylib.a
--libraylib.dylib
-main.c
有人可以帮我解释为什么会出现链接器错误并分享一些解决方法吗?
我将不胜感激任何帮助。
谢谢你。