简体   繁体   中英

Including SOIL2 library in a CMake project

I'm trying to include SOIL2 in my C++ OpenGL project.

So far I've

  • downloaded the library from https://github.com/SpartanJ/SOIL2
  • built it by running premake 4 and make.
  • copied the src/SOIL2/ directory to /usr/local/include
  • tried adding libsoil2-debug.a to /usr/local/lib
  • followed various examples of ways to include libraries with CMake

Platform is MacOS (Catalina) I'm still new to CMake, so I'm pretty sure that's where my problem is.

At the moment, my CMakeLists file looks like this:

cmake_minimum_required(VERSION 3.8)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 99)
set(This ComputerGraphicsProgramming)
project(${This} CXX C)

file(GLOB_RECURSE SOURCES src/*.cpp)
file(GLOB_RECURSE SOIL2_SOURCES /usr/local/include/SOIL2/*.c)
add_executable(${This} ${SOURCES} ${HEADERS})

include_directories(
  include
  lib
  /usr/local/include
)
link_directories(
  /usr/local/include
  /usr/local/include/SOIL2
)

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glew REQUIRED)
find_package(glm REQUIRED)
find_library(soil2-debug REQUIRED)

target_link_libraries(${This}
  GLEW::GLEW
  ${OPENGL_LIBRARIES} glfw
  soil2-debug
)

Texture.hpp, the file where I'm including SOIL, looks like this:

#include <GL/glew.h>
#include <SOIL2/SOIL2.h>
#include <string>

class Texture
{
  public:
    Texture(std::string filename);
    ~Texture();

  private:
    unsigned int m_ID;
};

And these are my errors when I run make:

$ make
[  9%] Linking CXX executable ComputerGraphicsProgramming
ld: library not found for -lsoil2-debug
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [ComputerGraphicsProgramming] Error 1
make[1]: *** [CMakeFiles/ComputerGraphicsProgramming.dir/all] Error 2
make: *** [all] Error 2

Any help appreciated!

looks like the libsoil2-debug.a couldn't be found. Try to add /usr/local/lib into your CMAKE_PREFIX_PATH :

list(APPEND CMAKE_PREFIX_PATH "/usr/local/lib")

somewhere on the top of your CMakeList.txt file

Finally got project to build, using the following CMakeLists file. Thanks for the replies!

cmake_minimum_required(VERSION 3.8)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 99)
set(This ComputerGraphicsProgramming)

project(${This} CXX C)

file(GLOB_RECURSE SOURCES src/*.cpp)
add_executable(${This} ${SOURCES} ${HEADERS})

include_directories(
  include
  /usr/local/include
)

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(glew REQUIRED)
find_package(glm REQUIRED)
find_library(SOIL2 soil2-debug)

target_link_libraries(${This}
  GLEW::GLEW
  ${OPENGL_LIBRARIES} glfw
  ${SOIL2}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM