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

时间:2017-11-03 16:20:20

标签: cmake soil

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

尝试:

我尝试使用标头只包含SOIL.h,然后在CMAKE中编译SOIL.c但我得到了类似于架构x86_64的未定义符号的错误,说它无法链接到SOIL_image_load和{{ 1}}功能。然后我继续在SOIL2页面上阅读指导使用库,然后指南说使用头文件并编译c文件但由于它不起作用,我按照其他方法提到在那里说使用premake来构建并使用SOIL_free_image文件,我已成功预先制作它并将所有文件复制到我的lib并将我的include目录更改为libsoil2.a并生成项目,但后来我收到以下错误。

问题:构建项目会引发错误:

lib/SOIL2/src/SOIL2

CMake的:

[ 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运行正常,这是3行:

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 )

1 个答案:

答案 0 :(得分:0)

我发现这里的问题有助于我解决问题

[CMake link to external library]

我要解决的问题是:

<强> 1。查找图书馆

find_library( SOIL2 SOIL2 lib/libsoil2.a )

<强> 2。链接库

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