Skip to content

Instantly share code, notes, and snippets.

@kheaactua
Last active April 9, 2018 13:33
Show Gist options
  • Save kheaactua/bcee42148faa063655c5042102938a1f to your computer and use it in GitHub Desktop.
Save kheaactua/bcee42148faa063655c5042102938a1f to your computer and use it in GitHub Desktop.
WML Linking in MSVC: Minimal Working Example

Minimal working example demonstrating my issue linking to my shared Wml3 lib.

Note, the WML3 code is not available in this Gist.

Build

Microsoft Visual Studio 12 2013

cmake -G"Visual Studio 12 2013 Win64" <source>

Open the project with Microsoft Visual Studio 12 2013, and build the testapp target.

Expected Result

Error	2	error LNK1120: 1 unresolved externals	C:\Users\mrussell\workspace\wml-min-example\bld\Release\testapp.exe	testapp
Error	1	error LNK2019: unresolved external symbol "public: static double const Wm3::Math<double>::DEG_TO_RAD" (?DEG_TO_RAD@?$Math@N@Wm3@@2NB) referenced in function main	C:\Users\mrussell\workspace\wml-min-example\bld\main.obj	testapp

Linux

cmake -GNinja -DCMAKE_BUILD_TYPE=Release <source> && ninja

(To use Makefiles, simply leave out the -GNinja)

Expected Result

Builds and runs without issue

cmake_minimum_required(VERSION 3.1)
project(wmlexample)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CONFIGURATION_TYPES "Release")
if (WIN32)
set(CONAN_WML_ROOT C:/Users/mrussell/.conan/data/wml/3.x/ntc/stable/package/a4501f33ae09df332b76b4d6f0e5cebffbe83874 CACHE PATH "Path to WML")
set(CONAN_BIN_DIRS_WML ${CONAN_BASE_DIRS_WML}/bin)
else()
set(CONAN_WML_ROOT "/home/matt/.conan/data/wml/3.x/ntc/stable/package/aa0498c70db8439f7f74687e9366567b85010fa5" CACHE PATH "Path to WML")
endif()
set(CONAN_INCLUDE_DIRS_WML ${CONAN_WML_ROOT}/include)
set(CONAN_LIB_DIRS_WML ${CONAN_WML_ROOT}/lib)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
find_package(Wml3 REQUIRED)
add_executable(testapp main.cpp)
target_link_libraries(testapp WML)
# vim: ts=4 sw=4 sts=0 expandtab ffs=unix,dos :
include(FindPackageHandleStandardArgs)
if (NOT Wml3_FOUND)
set(Wml3_INCLUDE_DIR ${CONAN_INCLUDE_DIRS_WML})
set(Wml3_LIBRARY_DIR ${CONAN_LIB_DIRS_WML})
find_library(Wml3_LIBRARY
NAMES Wml3
PATHS ${Wml3_LIBRARY_DIR}
NO_DEFAULT_PATHS
)
if (NOT Wml3_LIBRARY)
find_file(Wml3_LIB_SHARED
NAMES Wml3.dll
PATHS ${CONAN_BIN_DIRS_WML}
NO_DEFAULT_PATHS
)
endif()
find_package_handle_standard_args(Wml3 DEFAULT_MSG
Wml3_LIBRARY Wml3_INCLUDE_DIR
)
mark_as_advanced(Wml3_INCLUDE_DIR Wml3_LIBRARY)
if(NOT TARGET WML)
message(STATUS "Found WML:")
message(STATUS " include: ${CONAN_INCLUDE_DIRS_WML}")
add_library(WML INTERFACE)
set_property(TARGET WML PROPERTY INTERFACE_INCLUDE_DIRECTORIES
${CONAN_INCLUDE_DIRS_WML}
)
set_property(TARGET WML PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CONAN_INCLUDE_DIRS_WML})
if (Wml3_LIBRARY)
message(STATUS " libs: ${Wml3_LIBRARY}")
target_link_libraries(WML INTERFACE ${Wml3_LIBRARY})
list(APPEND EXTERNAL_LINK_DIRS ${Wml3_LIBRARY_DIR})
endif()
if (Wml3_LIB_SHARED)
message(STATUS " DLLs: ${Wml3_LIB_SHARED}")
list(APPEND EXTERNAL_LINK_DIRS ${CONAN_BIN_DIRS_WML})
endif()
set(Wml3_FOUND TRUE)
endif()
endif()
# vim: ts=4 sw=4 sts=0 expandtab ffs=unix :
#include <iostream>
#include "Wml3/Wm3Math.h"
auto main(int argc, char *argv[]) -> int
{
const auto d_to_r = Wm3::Math<double>::DEG_TO_RAD;
std::cout << "Degrees to Radians = " << d_to_r << " radians" << std::endl;
return 0;
}
/* vim: set ts=4 sw=4 sts=4 expandtab ffs=unix,dos : */
// ...
// for a DLL library
#if defined(WM3_DLL_EXPORT)
#define WM3_ITEM __declspec(dllexport)
// for a client of the DLL library
#elif defined(WM3_DLL_IMPORT)
#define WM3_ITEM __declspec(dllimport)
// for a static library
#else
#define WM3_ITEM
#endif
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment