Skip to content

C++: Linker errors when building as a static library on Windows #1326

@asahtik

Description

@asahtik

Description

  • Version: commit 0828ded
  • Platform: Windows 10, C++

I am attempting to build mcap as a static library and link it to a project (similarly to mcap_builder where I ran into the same issue). Everything works as expected on Linux, however on Windows I get multiple warnings like the following:

C:\Users\user\Documents\mcap_win_err_mre\mcap\include\mcap\writer.hpp(475,32): warning C4251: 'mcap
::McapWriter::attachmentIndex_': class 'std::vector<mcap::AttachmentIndex,std::allocator<mcap::AttachmentInde
x>>' needs to have dll-interface to be used by clients of class 'mcap::McapWriter' [C:\Users\user\Documents\mcap_win_err_mre\build\mcap.vcxproj]

followed by linker errors:

mcap_test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl mcap::M
capReader::close(void)" (__imp_?close@McapReader@mcap@@QEAAXXZ) referenced in function main [C:\Users\user\
Documents\mcap_win_err_mre\build\mcap_test.vcxproj]

I only get errors if I build statically. If I change add_library (mcap STATIC "mcap.cpp") to add_library (mcap SHARED "mcap.cpp") everything seems to work as expected.

Possibly related issue

Steps To Reproduce

MRE

On Windows:

git clone https://github.com/asahtik/mcap_win_err_mre.git
cd mcap_win_err_mre
cmake -S. -Bbuild
cmake --build build

Expected Behavior

Code compiles and links successfully without errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions