使用CMake和Visual Studio获取“未解析的外部符号”错误

问题描述 投票:0回答:1

我正在尝试使用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个回答
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显式处理单独的变量,并且包含目录和预处理器定义,以及在您希望使用库的每个位置。链接已经设置的导入库目标将自动导致必要的包含目录等配置...

© www.soinside.com 2019 - 2024. All rights reserved.