Goal: Trying to use SOIL2 to simply load image in OpenGL.
Attempt:
I have tried using headers only by including the SOIL.h and then compiling SOIL.c in CMAKE but I got similar error of Undefined symbols for architecture x86_64, said that It could not link to SOIL_image_load
and SOIL_free_image
function. Then I went on reading on SOIL2 page on guiding to use the library, then the guide said to use headers and compile the c file but since it did not work, I follow the other method which also mention in there said to use premake to build and use the libsoil2.a
file, which i have successfully premake it and copy all the files to my lib and change my include directory to lib/SOIL2/src/SOIL2
and make the project but then i got the following error.
Problem: Building project throws me error:
[ 33%] Building CXX object CMakeFiles/modern-gl-sdl.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/modern-gl-sdl.dir/include/shaders/Shader.cpp.o
[100%] Linking CXX executable bin/modern-gl-sdl
Undefined symbols for architecture x86_64:
"_CFBundleCreate", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
"_CFBundleGetFunctionPointerForName", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
"_CFRelease", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
"_CFStringCreateWithCString", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
"_CFURLCreateWithFileSystemPath", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
"___CFConstantStringClassReference", referenced from:
CFString in libsoil2.a(SOIL2.o)
"_kCFAllocatorDefault", referenced from:
_SOIL_GL_GetProcAddress in libsoil2.a(SOIL2.o)
_SOIL_GL_ExtensionSupported in libsoil2.a(SOIL2.o)
_query_gen_mipmap_capability in libsoil2.a(SOIL2.o)
_query_DXT_capability in libsoil2.a(SOIL2.o)
_query_PVR_capability in libsoil2.a(SOIL2.o)
_query_ETC1_capability in libsoil2.a(SOIL2.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [bin/modern-gl-sdl] Error 1
make[1]: *** [CMakeFiles/modern-gl-sdl.dir/all] Error 2
make: *** [all] Error 2
CMAKE:
cmake_minimum_required( VERSION 3.8 FATAL_ERROR )
set( CMAKE_BUILD_TYPE Debug )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14" )
project( modern-gl-sdl )
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(SDL2 REQUIRED)
find_library(COCOA Cocoa)
if (NOT COCOA)
message(FATAL_ERROR "Cocoa not found")
endif()
include_directories(
include
lib/SOIL2/src/SOIL2
${GLEW_INCLUDE_DIR}
${SDL2_INCLUDE_DIR}
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
file(
GLOB SOIL2_C
"*.c"
)
add_executable(
modern-gl-sdl
src/main.cpp
include/shaders/Shader.cpp
${SOIL2_C}
)
target_link_libraries(
modern-gl-sdl
${CMAKE_BINARY_DIR}/lib/SOIL2/lib/macosx/libsoil2.a
${SDL2_LIBRARIES}
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${COCOA_LIBRARIES}
)
add_custom_command( TARGET modern-gl-sdl POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
src/shaders
bin/shaders )
The cmake runs fine when I comment out the code that used to load the image texture which is this 3 lines:
unsigned char *image = SOIL_load_image ( "resources/images/sexy-girl.jpeg", &width, &height, &rchanel, 0 );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image );
SOIL_free_image_data ( image );
I found the issue here helps me solved the problem
[ CMake link to external library ]
Step I do to fix my problem was:
1. Find Library
find_library( SOIL2 SOIL2 lib/libsoil2.a )
2. Link Library
target_link_libraries ( modern-gl-sdl ... ${SOIL2} )
Done that is how i fixed my problem and hope it helps other with this same kind of problem and here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(modern_gl_sdl)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
find_package(SDL2 REQUIRED)
find_library(SOIL2 SOIL2 lib/libsoil2.a)
find_package(GLEW REQUIRED)
find_package(OPENGL REQUIRED)
if(APPLE)
find_library(COCOA Cocoa)
if (NOT COCOA)
message(FATAL_ERROR "Cocoa not found")
endif()
ENDIF(APPLE)
include_directories(
include
lib
${GLEW_INCLUDE_DIR}
)
set(SOURCE_FILES src/main.cpp include/Shader/Shader.cpp include/Shader/Shader.h)
add_executable(modern_gl_sdl
${SOURCE_FILES} include/Shader/Shader.cpp include/Shader/Shader.h)
target_link_libraries(modern_gl_sdl
${OPENGL_LIBRARY}
${SOIL2}
${COCOA_LIBRARY}
${GLEW_LIBRARY}
${SDL2_LIBRARY})