Project compiling with raylib static library - c++

I am trying to create a game with the Raylib library. I just was until now just trying around inside the main.cpp and always compiled my code with this line inside the terminal.
clang -std=c++11 -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL libraylib.a main.cpp -o my_app
Now I want to recreate the Snake game just for fun. I thought I would be wise the spilt the main.cpp into multiple src files but I actually don't know how I should compile all of this. I tried to create a CMakeList.txt like this:
cmake_minimum_required (VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework CoreVideo")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework IOKit")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework Cocoa")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework GLUT")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework OpenGL")
project(Snake)
add_executable(
Snake
main.cpp
Snake.cpp
)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/librarylib.a)
but this doesn't work. This is the Error message.
CMake Error at CMakeLists.txt:18 (target_link_libraries):
Cannot specify link libraries for target "main" which is not built by this
project.
-- Configuring incomplete, errors occurred!
Are there better ways to compile this project and can anyone explain to me what the -framework flag actually does? I still want to use VSCode because I want to learn how to use the terminal a bit better.
Edit: Information form brew ls --verbose raylib:
/usr/local/Cellar/raylib/2.5.0/LICENSE.md
/usr/local/Cellar/raylib/2.5.0/INSTALL_RECEIPT.json
/usr/local/Cellar/raylib/2.5.0/.brew/raylib.rb
/usr/local/Cellar/raylib/2.5.0/CHANGELOG
/usr/local/Cellar/raylib/2.5.0/include/raylib.h
/usr/local/Cellar/raylib/2.5.0/HISTORY.md
/usr/local/Cellar/raylib/2.5.0/README.md
/usr/local/Cellar/raylib/2.5.0/lib/pkgconfig/raylib.pc
/usr/local/Cellar/raylib/2.5.0/lib/cmake/raylib/raylib-config-version.cmake
/usr/local/Cellar/raylib/2.5.0/lib/cmake/raylib/raylib-config.cmake
/usr/local/Cellar/raylib/2.5.0/lib/libraylib.2.5.0.dylib
/usr/local/Cellar/raylib/2.5.0/lib/libraylib.2.dylib
/usr/local/Cellar/raylib/2.5.0/lib/libraylib.a
/usr/local/Cellar/raylib/2.5.0/lib/libraylib.dylib

Solved it by setting my CMakeLists.txt up like this:
cmake_minimum_required(VERSION 3.0)
project(Snake)
set (CMAKE_CXX_STANDARD 11)
# Executable & linking
add_executable(${PROJECT_NAME} main.cpp snake.cpp)
if (NOT TARGET raylib)
find_package(raylib 2.5.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)

You want to link your executable to librarylib.a. The target name of your executable is Snake.
If we look at the error:
Cannot specify link libraries for target "main" which is not built by this project.
Indeed, you never added a library or an executable called main.
This is what should be in your target_link_libraries call should look like:
target_link_libraries(Snake PUBLIC "${CMAKE_SOURCE_DIR}/librarylib.a")

Related

How does CMake handle shared-library upgrades?

I have a project where I link to boost; the CMakeLists.txt looks like this:
project(test VERSION 1.0)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op -Wredundant-decls -Wshadow -Woverloaded-virtual")
find_package(Boost COMPONENTS filesystem program_options log log_setup REQUIRED)
include_directories(
${Boost_INCLUDE_DIRS}
)
add_executable(test src/test.cpp)
target_link_libraries(test
LINK_PUBLIC
${Boost_LIBRARIES}
config++
)
install(TARGETS test DESTINATION /usr/bin)
Now I am stumped how the linking in CMake works under the hood. It compiles fine but after I upgraded Boost, I am getting the error
test: error while loading shared libraries: libboost_program_options.so.1.75.0: cannot open shared object file: No such file or directory
and I'd like to avoid having to recompile every time a library gets an update.
The CMake doc says that
The LINK_PUBLIC and LINK_PRIVATE modes can be used to specify both the link dependencies and the link interface in one command.
But I don't quite understand how or why CMake links to the specific version of Boost and not simply to /usr/lib/libboost_program_options.so which is just a symlink to the currently installed version.

How to fix: 'can not be used when making a shared object; recompile with -fPIC' using Cmake. Using plain g++ works

I get a message 'can not be used when making a shared object; recompile with -fPIC'
I have try other examples and the issue is the same.
I have try
changing from MODULE to SHARED
cmake .. -DCMAKE_CXX_FLAGS=-fPIC
and other variations
this works:
c++ -c -fPIC -I/usr/include/python3.6m ../account.cpp
c++ -shared -Wall -Werror -Wl,--export-dynamic account.o -L/usr/local/lib -lboost_python36 -o account.so
Here is the basic cmake
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Using: make VERBOSE=1 the output commands are:
c++ -DBOOST_ALL_NO_LIB -Daccount_EXPORTS -I/usr/include/python3.6m -isystem /usr/local/include -fPIC -o CMakeFiles/account.dir/account.cpp.o -c /src/boost_python_example/account.cpp
c++ -fPIC -shared -Wl,-soname,account.so -o account.so CMakeFiles/account.dir/account.cpp.o /usr/local/lib/libboost_python36.a
So the cmake is not getting the same paths and flags, I am learning cmake so Im trying to understand this problem. Clearly the problem is not on actual libs but telling cmake where to find the proper ones.
The solution was quite simple. Comparing both commands what was missing on the cmake command was: --export-dynamic
So I solved using option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON) interesting enough the comment is needed.
Working solution:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Thanks everyone for the comments they lead me to a solution

FreeType not linking with CMake after explicit path to library given.

I have an issue with linking FreeType with my project. I have my libs in a separate folder, {PROJECT}/lib, where libfreetype.a is located. My compiler is MinGW-64 4.8.3 x86_64-posix-seh-rev2, and I built libfreetype.a from source using this compiler. My cmake file looks like the following:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Build/Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(FREETYPE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include/)
set(FREETYPE_LIBRARY ${PROJECT_SOURCE_DIR}/lib/libfreetype.a)
find_package(Freetype REQUIRED)
set(SOURCE_FILES main.cpp ...)
add_executable(Wahoo ${SOURCE_FILES})
target_link_libraries(Test freetype)
Running this will cause the linker to fail with undefined references to '__imp_FT_Init_FreeType'. Removing the the libfreeType.a causes CMAKE say that it cannot find -lfreeType (as expected), but including libfreetype.a will cause the linker errors.
I have to change my answer, because I tried it on my own and it was bullshit.
I think, you have to change only your last line into
target_link_libraries(Test ${FREETYPE_LIBRARY})

Xcode link with glew error

I'm trying to write a project using OpenGL 4.1. I usually switch between Windows and OS X. On Windows I use Visual Studio 2013 making the project using cmake. This works just fine. On OS X, when I use cmake with -G "Unix Makefiles" I can compile and run just fine. However, if i generate an Xcode project, Xcode fails building with the error:
ld: library not found for -lglew
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm using this CMakeLists.txt.
cmake_minimum_required(VERSION 2.8.9)
project(Framework)
set(SRC_FILES src/shader.cpp
src/framework.cpp
src/fileio.cpp
src/scene.cpp
src/renderable.cpp
src/camera.cpp
src/mesh.cpp
src/material.cpp
src/light.cpp
src/triangle.cpp
src/cube.cpp
src/basicshader.cpp
src/frameworkmodel.cpp
src/texture.cpp
)
set(HEADER_FILES include/shader.h
include/framework.h
include/fileio.h
include/scene.h
include/renderable.h
include/camera.h
include/mesh.h
include/material.h
include/light.h
include/triangle.h
include/cube.h
include/basicshader.h
include/frameworkmodel.h
include/texture.h
)
if (WIN32)
list(APPEND SRC_FILES src/GLee.cpp src/glew.cpp)
list(APPEND HEADER_FILES include/GLee.h include/glew.h)
endif()
if (APPLE)
SET(GCC_COVERAGE_LINK_FLAGS "-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -lglew")
endif()
set(GLFW_PATH glfw-3.1)
set(SOIL_PATH SOIL)
add_subdirectory(${GLFW_PATH})
include_directories(${GLFW_PATH}/include)
include_directories(${SOIL_PATH})
file(GLOB SOIL_SRC
"${SOIL_PATH}/*.h"
"${SOIL_PATH}/*.c"
)
include_directories(./include)
add_library(soil ${SOIL_SRC})
add_library(framework ${SRC_FILES} ${HEADER_FILES})
target_link_libraries(framework soil)
add_executable(test01 tests/test01.cpp)
target_link_libraries(test01 framework)
target_link_libraries(test01 glfw ${GLFW_LIBRARIES})
target_link_libraries(test01 glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
target_link_libraries(test01 glfw ${GCC_COVERAGE_LINK_FLAGS})
Is there something obviously wrong with this?
SOLUTION
I found the .dylib location of my glew installation and added it to the xcode search path, works just as it should.

Cmake link library target link error

Hi I have problem with linkg Glfw and other libraries using cmake.
From command line i compile like this
g++ main.cpp -lGL -lGLU -lGLEW -lglfw
But I wanted to use cmake for compiling. I tried to use target_linkg_libraries but this produce error
CMake Error at CMakeLists.txt:18 (target_link_libraries): Cannot
specify link libraries for target "GL" which is not built by this
project.
I tried do this using add definitions. I dont see error but this don't link libraries.
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
ADD_DEFINITIONS(
-lGL
-lGLU
-lGLEW
-lglfw
)
add_executable(test.out
main.cpp
)
target_link_libraries(GL GLU GLEW glfw)
The syntax for target_link_libraries is:
target_link_libraries(your_executable_name libraries_list)
And you don't have to add add_definition statements (target_link_libraries adds this options)
There are also some useful variables provided by OpenGL and GLEW packages.
Your CMakeLists.txt should be like:
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_executable(test
main.cpp
)
target_link_libraries(test ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})
One important detail to keep in mind is to place the target_link_libraries after the add_executable (or add_library) line.