1

我正在尝试学习 C,但我正在努力理解链接。我无法使用 raylib 库编译我的 main.c 文件。

生成文件

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.o

main.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
4

1 回答 1

1

在所有目标文件之后,您必须将库放在链接行的末尾。另外,-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)
于 2020-06-17T23:44:56.077 回答