目前,我正在Windows 10下开发一个使用不同库的小型应用程序。首先,我想说,我已经做了好几年的专业开发人员了,但是我总是在不同的环境(主要是GNU/Linux和MacOS)上开发,对于我来说,它总是更像游戏操作系统而不是工作的操作系统;现在Windows 10似乎更加开放源码友好,我想试一试,看看结合WSL-2和vcpkg,我能不能有一些有用的东西。所以我觉得用快板库开发一个简单的跨平台的2D应用程序可能是个好主意。
我使用VisualStudio2019CommunityEdition创建了一个新的CMake项目,并在项目本身内设置了一个vcpkg克隆(我不觉得这是个好主意,但我有点喜欢把所有东西都放在一个地方)。使用vcpkg,我安装了我想要使用的两个库:快板和sqlite3。这两种安装都进行得很好,我实际上对vcpkg本身感到非常惊讶。
我设置了我的项目结构:由于这将是一个模块化的项目,我开始通过CMake函数在add_library(lib_name SHARED sources)
中创建一堆共享库,最后我得到了一个类似于以下结构的结构:
project_root/CMakeLists.txt
project_root/src/main.cpp
project_root/core
project_root/core/CMakeLists.txt
project_root/core/include/core/bunch_of_headers.hpp
project_root/core/src/bunch_of_sources.cpp
project_root/model
project_root/model/CMakeLists.txt
project_root/model/include/model/bunch_of_headers.hpp
project_root/model/src/bunch_of_sources.cpp
以此类推。
project_root
中的每个文件夹都包含一个模块,所有CMakeLists文件看起来都是如下所示:
add_library(model SHARED all_the_source_files.cpp)
include(DeclareNewSharedLibrary)
declare_new_shared_library(model)
find_package(sqlite3 CONFIG REQUIRED)
target_link_libraries(model sqlite3)
其中,declare_new_shared_library()
在单独的cmake模块中定义为以下函数
function(declare_new_shared_library lib_name)
message(STATUS "Declaring new library ${lib_name}")
include(GenerateExportHeader)
generate_export_header(${lib_name})
target_include_directories(${lib_name}
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}"
)
set_target_properties(${lib_name} PROPERTIES
IMPORTED_IMPLIB "${CMAKE_CURRENT_BINARY_DIR}/${lib_name}.lib"
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/${lib_name}.dll"
)
endfunction(declare_new_shared_library)
到目前为止还没什么好想的。
现在,每个模块都编译得很好,即使当我将共享库链接到通过vcpkg下载的库时也是如此(如上面的示例所示)。Windows上的ld
正确地输出所需的文件(.lib
和.dll
),并按预期将它们复制到${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
中。
我在这里遇到的问题是,有一个one库不编译。CMake文件与其他文件完全相同,只是我链接到了快板库(与其他文件一样,它是通过vcpkg安装的)。下面是用于比较的CMake文件:
add_library(core SHARED all_the_sources.cpp)
include(DeclareNewSharedLibrary)
declare_new_shared_library(core)
find_package(allegro CONFIG REQUIRED)
target_link_libraries(core allegro)
当我试图编译这个目标(无论是直接编译还是通过依赖关系系统编译)时,我会得到以下错误
TL;DR:
C:\Dev\ConsultantLife\msvcrtd.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
C:\Dev\ConsultantLife\bin\core.dll : fatal error LNK1120: 1 unresolved externals
下面是勇士们的长篇版本:
Cleaning... 3 files.
[1/2] C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\Llvm\80D306~1.0\bin\clang-cl.exe /nologo -TP -Dcore_EXPORTS -I..\..\..\core\include -Icore -I..\..\..\vcpkg\installed\x64-windows\include -m64 -fdiagnostics-absolute-paths /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /Focore\CMakeFiles\core.dir\Game.cpp.obj /Fdcore\CMakeFiles\core.dir\ -c ..\..\..\core\Game.cpp
[2/2] cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_dll --intdir=core\CMakeFiles\core.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx64\x64\link.exe /nologo core\CMakeFiles\core.dir\Game.cpp.obj /out:bin\core.dll /implib:core\core.lib /pdb:bin\core.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL ..\..\..\vcpkg\installed\x64-windows\debug\lib\allegro-debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D C:\Dev\ConsultantLife\out\build\x64-Debug\core && powershell -noprofile -executionpolicy Bypass -file C:/Dev/ConsultantLife/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/Dev/ConsultantLife/out/build/x64-Debug/bin/core.dll -installedDir C:/Dev/ConsultantLife/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
FAILED: bin/core.dll core/core.lib
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_dll --intdir=core\CMakeFiles\core.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100183~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx64\x64\link.exe /nologo core\CMakeFiles\core.dir\Game.cpp.obj /out:bin\core.dll /implib:core\core.lib /pdb:bin\core.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL ..\..\..\vcpkg\installed\x64-windows\debug\lib\allegro-debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cmd.exe /C "cd /D C:\Dev\ConsultantLife\out\build\x64-Debug\core && powershell -noprofile -executionpolicy Bypass -file C:/Dev/ConsultantLife/vcpkg/scripts/buildsystems/msbuild/applocal.ps1 -targetBinary C:/Dev/ConsultantLife/out/build/x64-Debug/bin/core.dll -installedDir C:/Dev/ConsultantLife/vcpkg/installed/x64-windows/debug/bin -OutVariable out""
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx64\x64\link.exe /nologo core\CMakeFiles\core.dir\Game.cpp.obj /out:bin\core.dll /implib:core\core.lib /pdb:bin\core.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL ..\..\..\vcpkg\installed\x64-windows\debug\lib\allegro-debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:core\CMakeFiles\core.dir/intermediate.manifest core\CMakeFiles\core.dir/manifest.res" failed (exit code 1120) with the following output:
Creating library core\core.lib and object core\core.exp
C:\Dev\ConsultantLife\msvcrtd.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
C:\Dev\ConsultantLife\bin\core.dll : fatal error LNK1120: 1 unresolved externals
ninja: build stopped: subcommand failed.
现在,据我所知,链接器正在尝试将我的库链接为可执行文件,因为它无法找到main
函数(是的,这是一个库,亲爱的.)它抱怨并给出一个错误。
项目中其他库的编译具有完全相同的link.exe
命令(除了系统库之外的不同库),而且不会失败。
现在我想问一问:我是否做错了什么,allegro-5.2
中是否有bug (尽管我不知道库如何查找主方法,链接器如何查找)。好吧,你明白我的意思了)还是我应该放弃一切?
这会是Allegro.cmake文件的问题吗?我不得不承认,我从未在专业项目中使用过CMake (我们是一群仍在使用GNU &Co.)的老家伙。所以我可能漏掉了什么。下面是我创建的文件(从vcpkg文件夹中找到的其他cmake文件复制/粘贴)
### Allegro-config.cmake
include(${CMAKE_CURRENT_LIST_DIR}/Allegro-targets.cmake)
### Allegro-targets.cmake
# Generated by CMake
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.5)
message(FATAL_ERROR "CMake >= 2.6.0 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.6)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_targetsDefined)
set(_targetsNotDefined)
set(_expectedTargets)
foreach(_expectedTarget sqlite3)
list(APPEND _expectedTargets ${_expectedTarget})
if(NOT TARGET ${_expectedTarget})
list(APPEND _targetsNotDefined ${_expectedTarget})
endif()
if(TARGET ${_expectedTarget})
list(APPEND _targetsDefined ${_expectedTarget})
endif()
endforeach()
if("${_targetsDefined}" STREQUAL "${_expectedTargets}")
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
return()
endif()
if(NOT "${_targetsDefined}" STREQUAL "")
message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_targetsDefined}\nTargets not yet defined: ${_targetsNotDefined}\n")
endif()
unset(_targetsDefined)
unset(_targetsNotDefined)
unset(_expectedTargets)
# Compute the installation prefix relative to this file.
set(_IMPORT_PREFIX "${PROJECT_SOURCE_DIR}/vcpkg/installed/${VCPKG_TARGET_TRIPLET}")
# Create imported target allegro
add_library(allegro SHARED IMPORTED)
set_target_properties(allegro PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
# Load information for each installed configuration.
file(GLOB CONFIG_FILES "${CMAKE_CURRENT_LIST_DIR}/Allegro-targets-*.cmake")
foreach(f ${CONFIG_FILES})
include(${f})
endforeach()
# Cleanup temporary variables.
set(_IMPORT_PREFIX)
# Loop over all imported files and verify that they actually exist
foreach(target ${_IMPORT_CHECK_TARGETS} )
foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )
if(NOT EXISTS "${file}" )
message(FATAL_ERROR "The imported target \"${target}\" references the file
\"${file}\"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
\"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
endif()
endforeach()
unset(_IMPORT_CHECK_FILES_FOR_${target})
endforeach()
unset(_IMPORT_CHECK_TARGETS)
# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)
### Allegro-targets-debug.cmake
#----------------------------------------------------------------
# Generated CMake target import file for configuration "Debug".
#----------------------------------------------------------------
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "allegro" for configuration "Debug"
set_property(TARGET allegro APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(allegro PROPERTIES
IMPORTED_IMPLIB_DEBUG "${_IMPORT_PREFIX}/debug/lib/allegro-debug.lib"
IMPORTED_LOCATION_DEBUG "${_IMPORT_PREFIX}/debug/bin/allegro-debug-5.2.dll"
)
list(APPEND _IMPORT_CHECK_TARGETS allegro )
list(APPEND _IMPORT_CHECK_FILES_FOR_allegro
"${_IMPORT_PREFIX}/debug/lib/allegro-debug.lib"
"${_IMPORT_PREFIX}/debug/bin/allegro-debug-5.2.dll"
)
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
由于这种行为在过去几天里一直让我抓狂,我在Linux环境中测试了相同的配置(vcpkg,cmake,.)它就像一种魅力,我把所有的东西都整理好了,它起了作用。我看到的唯一不同是,Linux上的vcpkg只使用静态库,而在Windows上使用dll。
对不起,文字墙!
谢谢你的帮助!
发布于 2019-09-24 19:13:46
通常情况下,我在寻求帮助几分钟后就能自己找到答案。
看起来,快板试图找到一个主要的方法,这就是为什么连接失败。
解决方案是将以下行添加到Allegro-config.cmake
文件中
target_compile_definitions(allegro INTERFACE ALLEGRO_NO_MAGIC_MAIN)
https://stackoverflow.com/questions/58085848
复制相似问题