CMake-使用glad / GLFW / Catch2的“体系结构x86_64的未定义符号”

时间:2018-11-07 14:35:01

标签: c++ cmake glfw catch2

所以,我有一个带有CMakeLists的项目,树如下所示:

proj dir:  
  --include
      window.h
      scene.h
      etc.  
  --src
      window.cpp
      scene.cpp
      main.cpp
      etc.
  --external
      --glad
      --SOIL
      --Catch2
  --tests
      CMakeLists.txt
      window_test.cpp
      main.cpp
 CMakeLists.txt

然后我尝试使用Catch2 lib在根测试项目中进行单元测试。但是当我尝试构建unit_test项目时,出现以下错误:

Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      mlg::System::Window::create() in libSimplePaint.a(window.cpp.o)
  "_glfwInit", referenced from:
      mlg::System::Window::Window(int, int, char const*) in libSimplePaint.a(window.cpp.o)
  "_glfwMakeContextCurrent", referenced from:
      mlg::System::Window::create() in libSimplePaint.a(window.cpp.o)
  "_glfwSetFramebufferSizeCallback", referenced from:
      mlg::System::Window::setFramebufferSizeCallback() const in libSimplePaint.a(window.cpp.o)
  "_glfwTerminate", referenced from:
      mlg::System::Window::close() const in libSimplePaint.a(window.cpp.o)
      mlg::System::Window::create() in libSimplePaint.a(window.cpp.o)
  "_glfwWindowHint", referenced from:
      mlg::System::Window::Window(int, int, char const*) in libSimplePaint.a(window.cpp.o)
  "_glfwWindowShouldClose", referenced from:
      mlg::System::Window::isOpen() const in libSimplePaint.a(window.cpp.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]: *** [SimplePaint_UnitTests] Error 1
make[1]: *** [CMakeFiles/SimplePaint_UnitTests.dir/all] Error 2
make: *** [all] Error 2

项目CMakeLists:

cmake_minimum_required(VERSION 3.11 FATAL_ERROR) 

project(SimplePaint)

set(CMAKE_CXX_FLAGS        "${CMAKE_CXX_FLAGS} -std=c++17")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo")

set(SRC_DIR     "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(LIBS_DIR    "${CMAKE_CURRENT_SOURCE_DIR}/external")
set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(BUILD_DIR   "${CMAKE_CURRENT_SOURCE_DIR}/build")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_DIR}/bin")

file(GLOB SOURCES "${SRC_DIR}/*.cpp" "${SRC_DIR}/*.vert" "${SRC_DIR}/*.frag")
file(GLOB HEADERS "${INCLUDE_DIR}/*.h" "${INCLUDE_DIR}/*.hpp")

add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIR})

# SOIL
include_directories(${soil_lib})
set(SOIL_LIBRARY ${LIBS_DIR}/SOIL/libsoil.a)
target_link_libraries(${PROJECT_NAME} ${SOIL_LIBRARY})

# glad
set(GLAD_DIR "${LIBS_DIR}/glad")
add_library("GLAD" "${GLAD_DIR}/src/glad.c")
target_include_directories("GLAD" PRIVATE "${GLAD_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include")
target_link_libraries(${PROJECT_NAME} "GLAD" "${CMAKE_DL_LIBS}")

单元测试CMakeLists:

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(SimplePaint_UnitTests)
# This module will be used for downloading third party code.
include(ExternalProject)

set(THIRDPARTY "${CMAKE_CURRENT_SOURCE_DIR}/../external")

ExternalProject_Add(
    Catch2

    GIT_REPOSITORY      "https://github.com/catchorg/Catch2.git"
    GIT_TAG             "master"

    SOURCE_DIR          "${THIRDPARTY}/Catch2-master"

    # Don't update and patch.
    UPDATE_COMMAND      ""
    PATCH_COMMAND       ""

    # Don't configure and build.
    CONFIGURE_COMMAND   ""
    BUILD_COMMAND       ""

    # Don't test and install.
    TEST_COMMAND        ""
    INSTALL_COMMAND     "")


set(Catch2_INCLUDE_DIR "${THIRDPARTY}/Catch2-master/single_include")

ExternalProject_Add(
    GLM

    GIT_REPOSITORY      "https://github.com/g-truc/glm.git"
    GIT_TAG             "0.9.4.6"

    SOURCE_DIR          "${THIRDPARTY}/glm-0.9.4.6"

    # Don't update and patch.
    UPDATE_COMMAND      ""
    PATCH_COMMAND       ""

    # Don't configure and build.
    CONFIGURE_COMMAND   ""
    BUILD_COMMAND       ""

    # Don't test and install.
    TEST_COMMAND        ""
    INSTALL_COMMAND     "")

set(GLM_INCLUDE_DIRS "${THIRDPARTY}/glm-0.9.4.6")

add_library(SimplePaint STATIC

    "${THIRDPARTY}/glad/src/glad.c"
    "${CMAKE_CURRENT_SOURCE_DIR}/../src/main.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/../src/window.cpp"
    )

target_include_directories(SimplePaint PUBLIC

    "${THIRDPARTY}/glad/include"
    "${CMAKE_CURRENT_SOURCE_DIR}/../include"
    "${CMAKE_CURRENT_SOURCE_DIR}/../src"
    "${GLM_INCLUDE_DIRS}"
    )

target_compile_features(SimplePaint PUBLIC cxx_std_17)

add_dependencies(SimplePaint GLM)

add_executable(${PROJECT_NAME}

    "${CMAKE_CURRENT_SOURCE_DIR}/window_test.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
    )

target_include_directories(${PROJECT_NAME} PRIVATE ${Catch2_INCLUDE_DIR})

target_link_libraries(${PROJECT_NAME} SimplePaint)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND "$<TARGET_FILE:${PROJECT_NAME}>")

add_dependencies(${PROJECT_NAME} Catch2)

我做错了什么?我包括GLFW,很高兴这样:

#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

如果我仅构建我的项目而没有进行单元测试,则所有工作和构建都将进行。编译器Clang 10.0

0 个答案:

没有答案