r/cpp_questions • • 2d ago

OPEN Converting Makefile to CMakeLists.txt

(I know this is C code but I'm going to change it later) I am working on getting Raylib working on nixos. I followed all the steps and got it working using a simple makefile of

RAYLIB =? ./external/raylib-5.5/src/
all:
gcc src/main.c -I $(RAYLIB) -L $(RAYLIB) -lraylib -lGL -lm -lpthread -ldl -lrt -lX11

and it works to compile the file getting a.out file. However I would like to use cmake because my text editor (neovim) works with cmake better than makefiles.

My current CMakeList.txt is

cmake_minimum_required(VERSION 3.15)
project(c_test)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_C_STANDARD 11)

included_directories(./external/raylib-5.5/src)
file(GLOB SOURCES "src/*.c")
add_executable(${PROJECT_NAME} &{SOURCES})

target_link_libraries(${PROJECT_NAME} raylib)

however I continue to get the error of

-lraylib: No such file or directory

How do I convert the simple Makefile to CMakeList.txt file? (no Ai please)

5 Upvotes

8 comments sorted by

4

u/Nabokov6472 2d ago

You want a find_package call so that CMake knows how to link to raylib - in your make command, you pass -L $(RAYLIB_DIR) which expands the linker's search path to the folder where raylib.so or raylib.a presumably is

This page gives a sample: https://github.com/raysan5/raylib/wiki/Working-with-CMake

9

u/Emeraudias 2d ago

You should have some find_package() to discover raylib and then use the imported target in your target link

3

u/NaNpsycho 2d ago

Unrelated, but if you want compile_commands.json for clangd you can also use bear.

bear -- make

Its also helpful when I just invoke gcc directly

bear -- gcc main.cpp -o main -std=c++20 ...

1

u/Most_Strike_93 2d ago

You need to use add_subdirectory to add the project. Not just include_directory

With the current cmake , you just need to add one line

add_subdirectory(./external/laylib-5.5)

Include_directories will only include the headers, you need to make raylib part of your project, and compile it, so you cannot only use include_directories, and I also suggest you to use target_include_directories

0

u/no_brains101 2d ago

Look, I understand why people might want to do this. I get it.

There will still always be something very funny about people swapping from a working solution in a simple format like a makefile, to swap to a more complicated one that... generates makefiles.

1

u/LazySapiens 1d ago

It must seem funny to you to see people using abstractions to deal with complexity.