tfussell / xlnt

:bar_chart: Cross-platform user-friendly xlsx library for C++11+
Other
1.49k stars 418 forks source link

_MSC_VER link errors in external project #561

Open pedro-vicente opened 3 years ago

pedro-vicente commented 3 years ago

when using the library with the static settings on a project

cmake ../../xlnt-1.5.0 -DSTATIC_CRT=ON -DSTATIC=ON

I have link errors

error LNK2019: unresolved external symbol "declspec(dllimport) public: cdecl xlnt::column_properties::column_properties(void)"

that happen because for some reason the symbol XLNT_API is defined as __declspec(dllimport)

here


#ifndef XLNT_API
#if !defined(XLNT_STATIC) && defined(_MSC_VER)
#ifdef XLNT_EXPORT
#define XLNT_API __declspec(dllexport)
#else
// For clients of the library, supress warnings about DLL interfaces for standard library classes
#pragma warning(disable : 4251)
#pragma warning(disable : 4275)
#define XLNT_API __declspec(dllimport)
#endif
#else
#define XLNT_API
#endif
#endif

Note: this just hapens on my project, not on the XLNT project itself, that builds fine

I tried just to hard code define it as

define XLNT_API

but got other linking errors because of inconsistent CRT use I use Multi-threaded Debug (/MTd) on my project but the library was using /MDd

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in qms_test.obj [E:\wx\qms\build\qms_test.vcxproj]

the only way to fix this was to use this call on the Cmake script

cmake_minimum_required(VERSION 3.17)
project(xlnt_all)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

that forces Multi-threaded Debug (/MTd) (requires Cmake 3.17)

and still hard code this

#define XLNT_API

pedro-vicente commented 3 years ago

I made a sample project that replicates the bug

https://github.com/pedro-vicente/xlnt_sample

it uses the STATIC settings with


git clone https://github.com/tfussell/xlnt xlnt-1.5.0

# clone missing dependency

pushd xlnt-1.5.0
pushd third-party
pushd libstudxml
git clone https://git.codesynthesis.com/libstudxml/libstudxml.git
popd
popd
popd

# build 

mkdir -p build/xlnt-1.5.0
pushd build

#XLS

pushd xlnt-1.5.0
cmake ../../xlnt-1.5.0 -DSTATIC_CRT=ON -DSTATIC=ON
cmake --build . 
popd

# example project

cmake .. -DBUILD_STATIC=ON
cmake --build . --parallel 9 
popd
lurker2021 commented 3 years ago

If your xlnt project ran well, then you got the file xlntd.lib & xlnt.lib, your project type should meet to them, such as x86 or x64, debug or release version.

In VS project, you could add it in stdafx.h like this: `

include "xlnt/xlnt.hpp"

ifdef _DEBUG

    #pragma comment (lib, "xlntd.lib")

else

    #pragma comment (lib, "xlnt.lib")

endif

` mismatch project type would cause this error. xlntd.dll and xlnt.dll were necessary for your project.