预期的 CMakeLists 正确设置为 lib 文件夹并编译构建项目所需的 c 文件,但得到未定义的符号体系结构

Expected CMakeLists correctly set to lib folder and compile the c file that needed to build the project, but got undefined symbol architecture

目标:尝试使用 SOIL2 在 OpenGL 中简单地加载图像。

尝试:

我尝试仅通过包含 SOIL.h 然后在 CMAKE 中编译 SOIL.c 来使用 headers 但我遇到了类似的架构未定义符号错误 x86_64,说它不能 link 到 SOIL_image_loadSOIL_free_image 函数。然后我继续阅读关于指导使用库的 SOIL2 页面,然后指南说使用 headers 并编译 c 文件但由于它不起作用,我按照其中也提到的其他方法说使用 premake 构建和使用 libsoil2.a 文件,我已经成功地预制作了它并将所有文件复制到我的 lib 并将我的 include 目录更改为 lib/SOIL2/src/SOIL2 并制作项目但随后出现以下错误.

问题:构建项目抛出错误:

[ 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 )

当我注释掉用于加载图像纹理的代码(这 3 行)时,cmake 运行良好:

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 );

我发现这里的问题帮我解决了问题

[CMake link to external library]

我解决问题的步骤是:

1.查找图书馆

find_library( SOIL2 SOIL2 lib/libsoil2.a )

2。 Link图书馆

target_link_libraries ( modern-gl-sdl ... ${SOIL2} )

这就是我解决问题的方法,希望它能帮助其他人解决同样的问题,这是我的 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})