I'm trying to use Allegro5 on Windows using CMake. My "CMakeLists.txt" file is
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})
My directory structure is:
root
|--CMakeLists.txt
|--main.cpp
|--build # this is where VS project files are generated.
|--dependencies
|--allegro
|--include
|--lib
|--bin
My 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;
}
The error I'm getting is
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 ==========
In case if it's useful, full linker progress logs:
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 ==========
EDIT:
Updated CMake file:
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)
If I'm understanding this correctly, then you're trying to pass DLL files to the linker. You cannot link a DLL directly in this way. You will need an import library to link against your DLL. Most likely, your Allegro build already comes with such an import library.
Also, consider using an IMPORTED library target for a more modern approach to represent the Allegro library inside your CMake build. For example:
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"
)
and then you just use it like
target_link_libraries(core … Allegro)
The nice thing about this approach is that it allows you to set up the library in one place and then just use it. No need to explicitly deal with separate variables for different kinds of libs and include directories and preprocessor definitions and whatnot at every point where you want to have something use the library. Linking an imported library target that has been set up like that will automatically cause the necessary include directories etc. to be configured…