链接器没有检测到我为 raylib 制作的静态库

Linker doesn't detect my static library made for raylib

提问人:pizzalawl 提问时间:8/21/2023 更新时间:8/21/2023 访问量:62

问:

我目前正在尝试创建一个小型库来在 raylib 中制作对话框。但是,当我尝试使用我的 Makefile 时,我收到此错误。请注意,我对 c++ 有点陌生!

/sbin/ld: cannot find -ldialog: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:14: test] Error 1

这是我的其余代码,因为我不确定是什么原因导致的,因为头文件和库文件分别位于 /usr/include 和 /usr/lib 中。

对话框.cpp -

#include <cstring>
#include <string>
#include <raylib.h>

using namespace std;

class DialogueBox {
    public:
    float thickness;
    Color box_background;
    char *contents;
    bool shadow;
    Rectangle rectangle;

    DialogueBox(float x, float y, float width, float height) {
        Rectangle rectangle = {x,y,width,height};
        contents = "Hello!";
    }

    void Draw(){
        if(shadow){
            DrawRectangle(rectangle.x + 10,rectangle.y + 10,rectangle.width,rectangle.height,GRAY);
        }
        DrawRectangleRec(rectangle, box_background);
        DrawRectangleLinesEx(rectangle, thickness, BLACK);

        for(int i = 0; i < strlen(contents); i++){
            char charBuffer[2] = {contents[i], '\0'};
            DrawText(std::string(1, contents[i]).c_str(), rectangle.x, rectangle.y, 5, BLACK);
        }
    }
};

dialog.h:

#ifndef DIALOGUE_BOX_H
#define DIALOGUE_BOX_H

#include <cstring>
#include <string>
#include <raylib.h>

class DialogueBox {
public:
    DialogueBox(float x, float y, float width, float height);

    void Draw();
    float thickness;
    Color box_background;
    char *contents;
    bool shadow;
    Rectangle rectangle;
};

#endif // DIALOGUE_BOX_H

测试.cpp:

#include "dialog.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);

    DialogueBox dialog(5, screenHeight/4, screenHeight/4 + screenHeight/4, screenWidth - 5);
    dialog.thickness = 5;
    dialog.box_background = GRAY;
    dialog.shadow = true;

    while (!WindowShouldClose())
    {
        BeginDrawing();

            ClearBackground(RAYWHITE);
            dialog.Draw();

        EndDrawing();
    }

    CloseWindow();

    return 0;
}

生成文件:

CC = gcc
CFLAGS = -O1
LIBS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11

all: dialog.o libdialog.a install test

dialog.o: dialog.cpp
    $(CC) $(CFLAGS) -c dialog.cpp -o dialog.o $(LIBS)

libdialog.a: dialog.o
    ar rcs libdialog.a dialog.o
    rm dialog.o
test: test.cpp
    $(CC) $(CFLAGS) test.cpp -o test $(LIBS) -ldialog

clean:
    rm /usr/bin/libdialog.a
    rm /usr/include/dialog.h
    rm libdialog.a
install: libdialog.a dialog.h
    cp libdialog.a /usr/bin
    cp dialog.h /usr/include

先谢谢你!

c++ makefile static-libraries linker-errors raylib

评论

0赞 MadScientist 8/21/2023
如果要在当前目录中搜索库,则需要添加到链接行中以告诉它在哪里查找。另请注意,不应添加到编译行(编译器行用于告诉编译器生成文件)。库仅在链接时使用。-L.$(LIBS)-c.o
0赞 Sam Varshavchik 8/21/2023
看起来它可能在构建之前尝试构建,因此它不存在,你会得到 bupkis。只需为 for 添加对 的显式直接依赖关系即可。maketestlibdialog.alibdialog.atest
0赞 pizzalawl 8/21/2023
你好!似乎问题在于链接器正在查找 /usr/local,而不是我预期的 /usr。现在,如果有人可以提供帮助,我会收到此错误: /sbin/ld: /tmp/ccoyZPeB.o: in function DialogueBox::D ialogueBox(float, float, float, float)' /sbin/ld: test.cpp:(.text+0x95): undefined reference to 'DialogueBox::D raw()' collect2:错误:ld 返回 1 退出状态main': test.cpp:(.text+0x59): undefined reference to
0赞 Botje 8/21/2023
默认情况下,-ldialog 会搜索动态库。尝试直接传递或在前面添加标志。libdialog.a-static-ldialog
0赞 Botje 8/21/2023
不相关:cpp 文件不应包含单独的类定义。它应该包括并仅将成员函数定义为独立函数(例如dialog.hvoid DialogueBox::draw() { ... }

答: 暂无答案