Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibTins cannot be statically linked because it using __declspec(dllimport) #11904

Closed
0blu opened this issue Jun 12, 2020 · 1 comment · Fixed by #22820
Closed

LibTins cannot be statically linked because it using __declspec(dllimport) #11904

0blu opened this issue Jun 12, 2020 · 1 comment · Fixed by #22820
Assignees
Labels
category:port-bug The issue is with a library, which is something the port should already support

Comments

@0blu
Copy link

0blu commented Jun 12, 2020

Describe the bug
A clear and concise description of what the bug is.

Environment

  • OS: Windows 10
  • Compiler: MSVC 14.26.28801

To Reproduce
vcpkg install libtins:x86-windows-static
or
vcpkg install libtins:x64-windows-static

main.cpp

#include <tins/tins.h>

int main() {
    Tins::NetworkInterface::default_interface();

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(staticLibTinsTesting)

set(CMAKE_CXX_STANDARD 11)

add_executable(staticLibTinsTesting main.cpp)

find_package(Boost COMPONENTS date_time REQUIRED)
find_package(libtins CONFIG REQUIRED)

target_link_libraries(staticLibTinsTesting PRIVATE
        Boost::date_time
        tins
        )

Expected behavior
Expecting to successfully link libtins without any dll-requirements.

Failure logs
main.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class Tins::NetworkInterface __cdecl Tins::NetworkInterface::default_interface(void)" (__imp_?default_interface@NetworkInterface@Tins@@SA?AV12@XZ) referenced in function main staticLibTinsTesting.exe : fatal error LNK1120: 1 unresolved externals

@0blu
Copy link
Author

0blu commented Jun 14, 2020

Thanks to @ras0219-msft on Discord, he figured out that it is necessary to define #define TINS_STATIC before using #include <tins/tins.h>.
Unfortunately the Packet.lib is now not included and will cause link errors:

wpcap.lib(inet.obj) : error LNK2019: unresolved external symbol PacketGetAdapterNames referenced in function pcap_lookupdev
wpcap.lib(fad-win32.obj) : error LNK2001: unresolved external symbol PacketGetAdapterNames
wpcap.lib(inet.obj) : error LNK2019: unresolved external symbol PacketGetNetInfoEx referenced in function pcap_lookupnet
wpcap.lib(fad-win32.obj) : error LNK2001: unresolved external symbol PacketGetNetInfoEx
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetMinToCopy referenced in function pcap_setmintocopy_win32
wpcap.lib(pcap-new.obj) : error LNK2001: unresolved external symbol PacketSetMinToCopy
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetMode referenced in function pcap_setmode_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetReadTimeout referenced in function pcap_setnonblock_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetBpf referenced in function pcap_setfilter_win32_npf
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketGetStats referenced in function pcap_stats_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetBuff referenced in function pcap_setbuff_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketGetNetType referenced in function pcap_activate_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketOpenAdapter referenced in function pcap_activate_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSendPacket referenced in function pcap_inject_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketAllocatePacket referenced in function pcap_inject_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketInitPacket referenced in function pcap_inject_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketFreePacket referenced in function pcap_inject_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketReceivePacket referenced in function pcap_read_win32_npf
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketSetHwFilter referenced in function pcap_activate_win32
wpcap.lib(pcap-win32.obj) : error LNK2019: unresolved external symbol PacketCloseAdapter referenced in function pcap_cleanup_win32
wpcap.lib(pcap.obj) : error LNK2019: unresolved external symbol PacketGetVersion referenced in function pcap_lib_version
wpcap.lib(pcap-new.obj) : error LNK2019: unresolved external symbol PacketSetLoopbackBehavior referenced in function pcap_open
staticLibTinsTesting.exe : fatal error LNK1120: 19 unresolved externals

Manually setting the Packet.lib path and adding the required Version.lib will cause already defined-Errors

Packet.lib(Packet32.obj) : error LNK2005: LoadLibrarySafe already defined in wpcap.lib(pcap-tc.obj)
Packet.lib(Packet32.obj) : error LNK2005: DllMain already defined in wpcap.lib(pcap-win32.obj)
LIBCMTD.lib(initializers.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
staticLibTinsTesting.exe : fatal error LNK1169: one or more multiply defined symbols found

Updates files:

main.cpp

#define TINS_STATIC
#include <tins/tins.h>

int main() {
    const auto interface = Tins::NetworkInterface::default_interface();

    Tins::Sniffer sniffer(interface.name());

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(staticLibTinsTesting)

set(CMAKE_CXX_STANDARD 11)

add_executable(staticLibTinsTesting main.cpp)

find_package(Boost COMPONENTS date_time REQUIRED)
find_package(libtins CONFIG REQUIRED)

target_link_libraries(staticLibTinsTesting PRIVATE
        Boost::date_time
        [...]/vcpkg/installed/x64-windows-static/lib/Packet.lib
        Version # Required by Packet
        tins
        )
set_property(TARGET staticLibTinsTesting PROPERTY
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

@LilyWangL LilyWangL added the category:port-bug The issue is with a library, which is something the port should already support label Jun 15, 2020
@PhoebeHui PhoebeHui assigned PhoebeHui and unassigned LilyWangL Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:port-bug The issue is with a library, which is something the port should already support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants