Cmake在Windows上可以正常工作,但是当程序在Linux(Ubuntu)中运行时会给出链接器错误。

时间:2018-11-21 02:13:56

标签: c++ linux cmake

我正在尝试用C ++编写OpenGL游戏和引擎应用程序。到目前为止,我已经使其在Windows以及Mac环境中都能完美运行。

但是,当我在Linux机器上尝试时,它给了我一个链接器错误,它找不到我在引擎库中定义的符号:

CMakeFiles/Game.dir/Game/Building.cpp.o: In function "v_game::Building::Building
(v_game::BuildingType, glm::vec<3, float, (glm::qualifier)0>)":
/home/sravan/Projects/Vermin/Game/Building.cpp:16: undefined reference to "vermi
n::Entity::Entity(std::__cxx11::basic_string<char, std::char_traits<char>, std::
allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char
>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_tra
its<char>, std::allocator<char> > const&, glm::vec<3, float, (glm::qualifier)0>,
 glm::vec<3, float, (glm::qualifier)0>)"
collect2: error: ld returned 1 exit status

目录结构如下:

Home
-- Docs
-- Engine
   -- EngineDeps
   -- Src
   -- cmake-build-debug
   -- CMakeLists.txt
-- Game
-- Shaders
-- cmake-build-debug
-- CMakeLists.txt
-- Assets

我的Engine / CMakeLists.txt看起来像这样。

cmake_minimum_required(VERSION 3.10)
project(Vermin)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# GLFW3 Includes

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(${CMAKE_SOURCE_DIR}/Engine/EngineDeps/Deps/glfw-3.2.1/)

#if(NOT MSVC)
# Assimp Stuff.
set(ASSIMP_BUILD_ASSIMP_TOOLS OFF CACHE BOOL "" FORCE)
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/assimp/)

message(STATUS ${CMAKE_LIBRARY_PATH})
#endif()

# Glad Stuff
add_library(GLAD ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/third_party/glad.c)
target_include_directories(GLAD PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Includes/)

# ImGUI Stuff
file(GLOB IMGUI_SRC
        ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/third_party/Imgui/*.cpp
        )

add_library(IMGUI
        ${IMGUI_SRC}
        )

target_include_directories(IMGUI
        PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/third_party/Imgui/
        ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Includes/
        )

file(GLOB VERMIN_HEADERS
        ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h
        )

file(GLOB VERMIN_SOURCE
        ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
        )

add_library(Vermin
        ${VERMIN_HEADERS}
        ${VERMIN_SOURCE}
        )

set(Includes
        "${CMAKE_CURRENT_SOURCE_DIR}/"
        "${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Includes/"
        "${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/third_party/Imgui/"
        "${OPENGL_INCLUDE_DIRS}"
        "${ASSIMP_INCLUDE_DIRS}"
        "${IMGUI_INCLUDE_DIRS}"
        )

message("${Includes}")

# Define the include DIRs
target_include_directories(Vermin PUBLIC ${Includes})

#if(MSVC)
#target_link_libraries(Vermin ${CMAKE_CURRENT_SOURCE_DIR}/EngineDeps/Deps/binaries/assimp.lib)
#endif()

#find_package(ASSIMP REQUIRED)

set(LIBS glfw ${OPENGL_LIBRARIES} GLAD assimp IMGUI)

target_link_libraries(Vermin ${LIBS})

find_package(OpenGL REQUIRED)

if ( NOT MSVC )
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
add_compile_options(Wa mbig-obj)
endif()
#add_compile_definitions(GLM_MESSAGES=1)

现在,我的主要CMakeLists文件如下,

cmake_minimum_required(VERSION 3.10)

project(Game)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory("Engine")

target_include_directories(IMGUI
        PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/Engine/src/
        )

add_executable(
        Game
        Game/main.cpp
        Game/Game.h
        Game/Game.cpp
        Game/GameScene.h
        Game/GameScene.cpp
        Game/GamePlayManager.cpp
        Game/GamePlayManager.h
        Game/Unit.cpp
        Game/Unit.h Game/Villager.cpp Game/Villager.h Game/Warrior.cpp Game/Warrior.h Game/Catapult.cpp Game/Catapult.h Game/Building.cpp Game/Building.h Game/Barracks.cpp Game/Barracks.h Game/SiegeFactory.cpp Game/SiegeFactory.h
        Game/MenuScene.h
        Game/MenuScene.cpp
)

target_link_libraries(Game Vermin)

这在Windows和Mac上均可完美运行。但是,一旦我转移到linux机器上,我将面临诸如未解决的符号,实体之类的链接器问题。 (在引擎库中定义)。

我缺少任何特定于Linux的命令吗?

我可以编译Engine Project。 我就是无法构建游戏项目。

我正在使用CMake和CLion构建项目。

如果任何人想看一下该项目的仓库,则位于https://github.com/sravankaruturi/Vermin

运行make时的错误日志:

[ 96%] Linking CXX executable Game
CMakeFiles/Game.dir/Game/Game.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Window>::construct<vermin::Window, int, int, char const (&) [9], bool>(vermin::Window*, int&&, int&&, char const (&) [9], bool&&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Window::Window(unsigned int, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
CMakeFiles/Game.dir/Game/GameScene.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Terrain>::construct<vermin::Terrain, int, int, double, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(vermin::Terrain*, int&&, int&&, double&&, double&&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Terrain::Terrain(int, int, float, float, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
CMakeFiles/Game.dir/Game/GamePlayManager.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Texture>::construct<vermin::Texture, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool>(vermin::Texture*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&, bool&&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Texture::Texture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
CMakeFiles/Game.dir/Game/GamePlayManager.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Texture>::construct<vermin::Texture, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool>(vermin::Texture*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool&&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Texture::Texture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
CMakeFiles/Game.dir/Game/GamePlayManager.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Object>::construct<vermin::Object, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&>(vermin::Object*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Object::Object(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/Game.dir/Game/GamePlayManager.cpp.o: In function `void __gnu_cxx::new_allocator<vermin::Object>::construct<vermin::Object, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(vermin::Object*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&)':
/usr/include/c++/7/ext/new_allocator.h:136: undefined reference to `vermin::Object::Object(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
CMakeFiles/Game.dir/Game/Unit.cpp.o: In function `v_game::Unit::Unit(v_game::UnitType)':
/home/sravan/Projects/Vermin/Game/Unit.cpp:12: undefined reference to `vermin::AnimatedEntity::AnimatedEntity(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, glm::vec<3, float, (glm::qualifier)0>, glm::vec<3, float, (glm::qualifier)0>)'
CMakeFiles/Game.dir/Game/Building.cpp.o: In function `v_game::Building::Building(v_game::BuildingType, glm::vec<3, float, (glm::qualifier)0>)':
/home/sravan/Projects/Vermin/Game/Building.cpp:16: undefined reference to `vermin::Entity::Entity(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, glm::vec<3, float, (glm::qualifier)0>, glm::vec<3, float, (glm::qualifier)0>)'
collect2: error: ld returned 1 exit status
CMakeFiles/Game.dir/build.make:262: recipe for target 'Game' failed
make[2]: *** [Game] Error 1
CMakeFiles/Makefile2:77: recipe for target 'CMakeFiles/Game.dir/all' failed
make[1]: *** [CMakeFiles/Game.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

g ++->版本7.3.0 cmake->版本3.10.2 CLion 2.6版。 2018。

0 个答案:

没有答案