使用 CMake 和 Visual Studio 时出现 "unresolved external symbol" 错误

Getting "unresolved external symbol" error with CMake and Visual Studio

我正在尝试使用 CMake 在 Windows 上使用 Allegro5。我的 "CMakeLists.txt" 文件是

cmake_minimum_required (VERSION 3.8)

project("My Project")

set(SOURCE_FILES "main.cpp")
add_executable(core ${SOURCE_FILES})

set(ALLEGRO_INCLUDE ".\dependencies\allegro\include")
set(ALLEGRO_LIB ".\dependencies\allegro\lib")
set(ALLEGRO_DYLIB ".\dependencies\allegro\bin\*.dll")

include_directories(${ALLEGRO_INCLUDE})
link_directories(${ALLEGRO_LIB})
file(GLOB LIBRARIES ${ALLEGRO_DYLIB})

target_link_libraries(core ${LIBRARIES} ${ALLEGRO_DYLIB})

我的目录结构是:

root
|--CMakeLists.txt
|--main.cpp
|--build # this is where VS project files are generated.
|--dependencies
      |--allegro
           |--include
           |--lib
           |--bin

我的main.cpp:

#include <iostream>
#include <allegro5/allegro.h>

int main(int argc, char *args[])
{
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;

    ALLEGRO_DISPLAY *window = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
    std::system("pause");
    return 0;
}

我得到的错误是

1>------ Build started: Project: core, Configuration: Debug Win32 ------
1>main.obj : error LNK2019: unresolved external symbol __imp__al_create_display referenced in function _main
1>~\root\build\Debug\core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "core.vcxproj" -- FAILED.
2>------ Skipped Build: Project: ALL_BUILD, Configuration: Debug Win32 ------
2>Project not selected to build for this solution configuration 
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 1 skipped ==========

如果它有用,完整的链接器进度日志:

     1>------ Build started: Project: core, Configuration: Debug Win32 ------
1>
1>Starting pass 1
1>Processed /DEFAULTLIB:msvcprtd
1>Processed /DEFAULTLIB:MSVCRTD
1>Processed /DEFAULTLIB:OLDNAMES
1>
1>Searching libraries
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib:
1>      Found __RTC_CheckEsp
1>        Referenced in main.obj
1>        Loaded MSVCRTD.lib(stack.obj)
1>      Found ___xi_a
1>        Referenced in MSVCRTD.lib(exe_main.obj)
1>        Loaded MSVCRTD.lib(initializers.obj)
1>Processed /DEFAULTLIB:kernel32.lib
1> Processed /DISALLOWLIB:msvcrt.lib
1> Processed /DISALLOWLIB:libucrtd.lib
1>      Found ___guard_check_icall_fptr
1>        Referenced in MSVCRTD.lib(exe_main.obj)
1>        Loaded MSVCRTD.lib(guard_support.obj)
1>      Found ___security_cookie
1>        Referenced in MSVCRTD.lib(gs_support.obj)
1>        Loaded MSVCRTD.lib(gs_cookie.obj)
1>      Found "int __cdecl _RTC_GetSrcLine(unsigned char *,wchar_t *,unsigned long,int *,wchar_t *,unsigned long)" (?_RTC_GetSrcLine@@YAHPAEPA_WKPAH1K@Z)
1>        Referenced in MSVCRTD.lib(error.obj)
1>        Loaded MSVCRTD.lib(gs_report.obj)
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\vcruntimed.lib:
1>      Found ___std_type_info_destroy_list
1>        Referenced in MSVCRTD.lib(tncleanup.obj)
1>        Loaded vcruntimed.lib(VCRUNTIME140D.dll)
1>      Found __IMPORT_DESCRIPTOR_VCRUNTIME140D
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Loaded vcruntimed.lib(VCRUNTIME140D.dll)
1>      Found __NULL_IMPORT_DESCRIPTOR
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Loaded vcruntimed.lib(VCRUNTIME140D.dll)
1>      Found VCRUNTIME140D_NULL_THUNK_DATA
1>        Referenced in vcruntimed.lib(VCRUNTIME140D.dll)
1>        Loaded vcruntimed.lib(VCRUNTIME140D.dll)
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\ucrt\x86\ucrtd.lib:
1>      Found __imp__system
1>        Referenced in main.obj
1>        Loaded ucrtd.lib(ucrtbased.dll)
1>      Found KERNEL32_NULL_THUNK_DATA
1>        Referenced in kernel32.lib(KERNEL32.dll)
1>        Loaded kernel32.lib(KERNEL32.dll)
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\MSVCRTD.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\ucrt\x86\ucrtd.lib:
1>
1>Finished searching libraries
1>
1>Searching libraries
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\MSVCRTD.lib:
1>      Found ___scrt_stub_for_acrt_initialize
1>        Loaded MSVCRTD.lib(ucrt_stubs.obj)
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\ucrt\x86\ucrtd.lib:
1>
1>Finished searching libraries
1>
1>Finished pass 1
1>
1>
1>Searching libraries
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\MSVCRTD.lib:
1>      Found __load_config_used
1>        Loaded MSVCRTD.lib(loadcfg.obj)
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\ucrt\x86\ucrtd.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib:
1>
1>Finished searching libraries
1>
1>Invoking rc.exe:
1> /v
1> /x
1> /fo
1> "C:\Users\xxxxx\AppData\Local\Temp\lnk{EE34E5F7-CFF6-4E0A-B531-A9BB467BAB4C}.tmp"
1> "C:\Users\xxxxx\AppData\Local\Temp\lnk{2C99769D-323C-4CED-9784-2C6DC54A8A80}.tmp"
1>Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
1>
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>
1>
1>Creating C:\Users\xxxxx\AppData\Local\Temp\lnk{EE34E5F7-CFF6-4E0A-B531-A9BB467BAB4C}.tmp
1>
1>Using codepage 1252 as default
1>
1>C:\Users\xxxxx\AppData\Local\Temp\lnk{2C99769D-323C-4CED-9784-2C6DC54A8A80}.tmp.
1>Writing 24:1, lang:0x409, size 381
1>
1>Invoking cvtres.exe:
1> /machine:x86
1> /verbose
1> /out:"C:\Users\xxxxx\AppData\Local\Temp\lnk{78C2B88C-BF43-4FC2-B0ED-A99EAE9D6EFC}.tmp"
1> /readonly
1> "C:\Users\xxxxx\AppData\Local\Temp\lnk{EE34E5F7-CFF6-4E0A-B531-A9BB467BAB4C}.tmp"
1>Microsoft (R) Windows Resource To Object Converter Version 14.16.27030.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>
1>adding resource. type:MANIFEST, name:1, language:0x0409, flags:0x30, size:381
1>
1>main.obj : error LNK2019: unresolved external symbol __imp__al_create_display referenced in function _main
1>
1>Unused libraries:
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\user32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\gdi32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\winspool.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\shell32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\ole32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\oleaut32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\uuid.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\comdlg32.lib
1>  C:\Program Files (x86)\Windows Kits\lib.0.17134.0\um\x86\advapi32.lib
1>  C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\msvcprtd.lib
1>  C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.16.27023\lib\x86\OLDNAMES.lib
1>
1>C:\Users\xxxxx\xxxxx\root\build\Debug\core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "core.vcxproj" -- FAILED.
2>------ Skipped Build: Project: ALL_BUILD, Configuration: Debug Win32 ------
2>Project not selected to build for this solution configuration 
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 1 skipped ==========

编辑:

已更新 CMake 文件:

set(SOURCE_FILES "main.cpp")
add_executable(core ${SOURCE_FILES})

add_library(Allegro DYNAMIC IMPORTED)
set_target_properties(Allegro PROPERTIES
    IMPORTED_LOCATION "${DEPENDENCIES_DIR}/allegro/bin/allegro.dll.a"
    IMPORTED_LOCATION "${DEPENDENCIES_DIR}/allegro/lib/allegro.lib"
)

target_include_directories(Allegro PUBLIC "${DEPENDENCIES_DIR}/allegro/include")
target_link_libraries(core Allegro)

如果我的理解正确,那么您正试图将 DLL 文件传递​​给 linker。您不能以这种方式直接 link DLL。你需要一个 import library 到 link 来对抗你的 DLL。很可能,您的 Allegro 构建已经带有这样的导入库。

此外,考虑使用 IMPORTED library target 更现代的方法来表示 CMake 构建中的 Allegro 库。例如:

add_library(Allegro SHARED IMPORTED)
set_target_properties(Allegro PROPERTIES
    IMPORTED_LOCATION "${DEPENDENCIES_DIR}/allegro/bin/allegro.dll"
    IMPORTED_IMPLIB "${DEPENDENCIES_DIR}/allegro/lib/allegro.lib"
    INTERFACE_INCLUDE_DIRECTORIES "${DEPENDENCIES_DIR}/allegro/include"
)

然后你就像

一样使用它
target_link_libraries(core … Allegro)

这种方法的好处在于它允许您在一个地方设置库,然后就可以使用它。无需为不同类型的库显式处理单独的变量,也无需在您希望使用库的每个地方包含目录和预处理器定义以及诸如此类的东西。链接已设置的导入库目标将自动导致配置必要的包含目录等…