0

I am currently trying to create a tiny library to make dialog boxes in raylib. When I try to use my Makefile, though, I get this error. Please note that I'm a bit new to c++!:

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

Here's the rest of my code as I am seriously not sure what could be causing it as the header and library files are both in /usr/include and /usr/lib respectively.

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

test.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;
}

Makefile:

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

Thank you in advance!

pizzalawl
  • 43
  • 8
  • If you want to search for libraries in the current directory you need to add `-L.` to the link line to tell it where to look. Also note you should not add `$(LIBS)` to your compile line (the compiler line uses `-c` to tell the compiler to generate a `.o` file). Libraries are only used at link time. – MadScientist Aug 20 '23 at 23:48
  • It looks like `make` is probably trying to build `test` before building `libdialog.a`, hence it doesn't exist, and you get bupkis. Just add an explicit, direct dependency on `libdialog.a`, for `test`. – Sam Varshavchik Aug 20 '23 at 23:57
  • Hello! Seems like the problem was that the linker was looking in /usr/local instead of /usr as I expected. Now I'm getting this error if anyone could help: /sbin/ld: /tmp/ccoyZPeB.o: in function `main': test.cpp:(.text+0x59): undefined reference to `DialogueBox::DialogueBox(float, float, float, float)' /sbin/ld: test.cpp:(.text+0x95): undefined reference to `DialogueBox::Draw()' collect2: error: ld returned 1 exit status – pizzalawl Aug 21 '23 at 02:34
  • -ldialog searches for a dynamic library by default. Try passing `libdialog.a` directly or adding the `-static` flag in front of `-ldialog`. – Botje Aug 21 '23 at 08:51
  • Unrelated: Your cpp file should not contain a separate class definition. It should include `dialog.h` and only define member functions as standalone functions (eg `void DialogueBox::draw() { ... }` – Botje Aug 21 '23 at 08:53
  • Also, removing `dialog.o` after the construction of `libdialog.a` forces Make to recompile every time, even if `dialog.cpp` has not changed. Installing stuff to `/usr` should not be your main way of testing (see the comment about `-L.` above). – Botje Aug 21 '23 at 08:53

0 Answers0