【问题标题】:Getting "unresolved external symbol" error with CMake and Visual Studio使用 CMake 和 Visual Studio 出现“未解析的外部符号”错误
【发布时间】:2019-08-28 07:27:21
【问题描述】:

我正在尝试使用 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\10\lib\10.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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\10\lib\10.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\10\lib\10.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\MSVCRTD.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x86\ucrtd.lib:
1>
1>Finished searching libraries
1>
1>Searching libraries
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.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\10\lib\10.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\msvcprtd.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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 Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\OLDNAMES.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\vcruntimed.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\ucrt\x86\ucrtd.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\kernel32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\user32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\gdi32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\winspool.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\shell32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\ole32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\oleaut32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\uuid.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\comdlg32.lib:
1>    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\advapi32.lib:
1>    Searching C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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\10\lib\10.0.17134.0\um\x86\user32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\gdi32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\winspool.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\shell32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\ole32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\oleaut32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\uuid.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\comdlg32.lib
1>  C:\Program Files (x86)\Windows Kits\10\lib\10.0.17134.0\um\x86\advapi32.lib
1>  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x86\msvcprtd.lib
1>  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.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)

【问题讨论】:

    标签: c++ windows cmake visual-studio-2017 allegro5


    【解决方案1】:

    如果我理解正确,那么您是在尝试将 DLL 文件传递​​给链接器。您不能以这种方式直接链接 DLL。您将需要一个import library 来链接您的 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)
    

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

    【讨论】:

    • 如果我有 .lib.dll,你能告诉我如何使用 CMake 为我的案例做一个导入库吗?
    • @swdon 在上面添加了这样一个示例。
    • 谢谢。当我尝试时,我收到错误“CMakeLists.txt:8 (add_library) 的 CMake 错误:没有给目标提供源:Allegro”。我认为它在 add_library Allegro 之后需要一个源文件?
    • @swdon 抱歉,我认为这只是一个错字,应该是 SHARED,而不是 DYNAMIC...
    • 确实,我刚刚尝试了 SHARED。然后我收到错误“target_include_directories 只能在 IMPORTED 目标上设置 INTERFACE 属性”。我认为这非常接近。有什么想法吗?
    猜你喜欢
    • 2023-03-28
    • 2021-08-02
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多