我正在努力学习C语言,我正在努力理解链接。我在使用raylib库编译main.c文件时遇到问题。
makefile
CFLAGS= -g -O -Wall -W -pedantic -std=c99 -O0
BASIC = -o -std=c99
LINKFLAGS=-I. -I/raylib/src -I../src -L/raylib/src -L/opt/vc/lib -lraylib -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -ldl -DPLATFORM_RPI
run:
gcc $(CFLAGS) $(LINKFLAGS) main.c -o main.omain.c文件
#include <stdio.h>
#include "raylib.h"
int main(void)
{
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
while (!WindowShouldClose()) // Detect window close button or ESC key
{
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
CloseWindow(); // Close window and OpenGL context
return 0;
}目录结构
Pong/
- main.c
- Makefile
- raylib/
- raylib.h但是当我运行make && ./main.o时,我得到了这个错误。尽管我在项目中有raylib.h文件和raylib文件夹。有没有人知道可能会发生什么?
gcc -g -O -Wall -W -pedantic -std=c99 -O0 -I. -I/raylib/src -I../src -L/raylib/src -L/opt/vc/lib -lraylib -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -ldl -DPLATFORM_RPI main.c -o main.o
/usr/bin/ld: /tmp/ccvCErhi.o: in function `main':
/home/pi/pong/main.c:12: undefined reference to `InitWindow'
/usr/bin/ld: /home/pi/pong/main.c:14: undefined reference to `SetTargetFPS'
/usr/bin/ld: /home/pi/pong/main.c:27: undefined reference to `BeginDrawing'
/usr/bin/ld: /home/pi/pong/main.c:29: undefined reference to `ClearBackground'
/usr/bin/ld: /home/pi/pong/main.c:31: undefined reference to `DrawText'
/usr/bin/ld: /home/pi/pong/main.c:33: undefined reference to `EndDrawing'
/usr/bin/ld: /home/pi/pong/main.c:18: undefined reference to `WindowShouldClose'
/usr/bin/ld: /home/pi/pong/main.c:39: undefined reference to `CloseWindow'
collect2: error: ld returned 1 exit status
make: *** [Makefile:6: run] Error 1发布于 2020-06-18 07:44:56
必须将库放在链接行的末尾,放在所有目标文件之后。此外,-I和-D是编译器标志,而不是链接器标志:
CFLAGS = -g -O -Wall -W -pedantic -std=c99 -O0 -I. -I/raylib/src -I../src -DPLATFORM_RPI
LDFLAGS = -L/raylib/src -L/opt/vc/lib
LDLIBS = -lraylib -lbrcmGLESv2 -lbrcmEGL -lpthread -lrt -lm -lbcm_host -ldl
run:
gcc $(CFLAGS) $(LDFLAGS) main.c -o main.o $(LDLIBS)https://stackoverflow.com/questions/62439961
复制相似问题