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

Windows build adjustment #716

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

novemus
Copy link

@novemus novemus commented Jul 9, 2024

Apart from the cmake script adjustment, there are some minor code changes to fix dll export.

Steps to build opendht with Visual Studio 17 2022.

  1. Install vcpkg and opendht dependencies (it takes a long time)
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg && bootstrap-vcpkg.bat
vcpkg install shiftmedia-libgnutls:x64-windows argon2:x64-windows asio:x64-windows fmt:x64-windows jsoncpp:x64-windows llhttp:x64-windows msgpack:x64-windows nettle:x64-windows openssl:x64-windows restinio:x64-windows
  1. Build the project
cd <opendht_root>
mkdir build && cd build
set PKG_CONFIG_PATH=<path_to_vcpkg_root>/installed/x64-windows/lib/pkgconfig
set CMAKE_PREFIX_PATH=<path_to_vcpkg_root>/installed/x64-windows/share
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
cmake -S .. -G "Visual Studio 17 2022" -T host=x64 -A x64
cmake --build . --config Release --target ALL_BUILD

Copy link
Member

@aberaud aberaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot :)
Could you please explain what you changed, and if possible split code changes and build system changes.

include/opendht/default_types.h Outdated Show resolved Hide resolved
include/opendht/infohash.h Outdated Show resolved Hide resolved
@novemus
Copy link
Author

novemus commented Jul 10, 2024

Thanks a lot :) Could you please explain what you changed, and if possible split code changes and build system changes.

In general, the windows build works fine, but only for static opendht target. When I try to build dynamyc library and link it with the tool's executables, I got the compilation error first

[build] D:\projects\opendht\include\opendht\infohash.h(254,41): error C2375: 'dht::operator <<': redefinition; different linkage [D:\projects\opendht\build\opendht.vcxproj]

This is solved by adding OPENDHT_PUBLIC attribute to the template definition. But maybe it is excess in the friendly function definitions. Then I got a few linking errors

[build] dhtscanner.obj : error LNK2019: unresolved external symbol getopt_long_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtscanner.vcxproj]
[build] dhtscanner.obj : error LNK2019: unresolved external symbol "struct dht::HexMap const dht::hex_map" (?hex_map@dht@@3UHexMap@1@B) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl dht::operator<<<20>(class std::basic_ostream<char,struct std::char_traits<char> > &,class dht::Hash<20> const &)" (??$?6$0BE@@dht@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBV?$Hash@$0BE@@0@@Z) [D:\projects\opendht\build\tools\dhtscanner.vcxproj]
[build] dhtscanner.obj : error LNK2019: unresolved external symbol optarg_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtscanner.vcxproj]
[build] dhtchat.obj : error LNK2019: unresolved external symbol getopt_long_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtchat.vcxproj]
[build] dhtchat.obj : error LNK2019: unresolved external symbol "struct dht::HexMap const dht::hex_map" (?hex_map@dht@@3UHexMap@1@B) referenced in function "public: char const * __cdecl dht::Hash<20>::to_c_str(void)const " (?to_c_str@?$Hash@$0BE@@dht@@QEBAPEBDXZ) [D:\projects\opendht\build\tools\dhtchat.vcxproj]
[build] dhtchat.obj : error LNK2019: unresolved external symbol "public: static struct dht::ValueType const dht::ImMessage::TYPE" (?TYPE@ImMessage@dht@@2UValueType@2@B) referenced in function "class dht::Value::Filter __cdecl dht::getFilterSet<class dht::ImMessage,0>(class dht::Value::Filter)" (??$getFilterSet@VImMessage@dht@@$0A@@dht@@YA?AVFilter@Value@0@V120@@Z) [D:\projects\opendht\build\tools\dhtchat.vcxproj]
[build] dhtchat.obj : error LNK2019: unresolved external symbol optarg_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtchat.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol __imp_htons referenced in function "public: __cdecl dht::IpServiceAnnouncement::IpServiceAnnouncement(unsigned short,unsigned short)" (??0IpServiceAnnouncement@dht@@QEAA@GG@Z) [D:\projects\opendht\build\tools\dhtnode.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol getopt_long_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtnode.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol "struct dht::HexMap const dht::hex_map" (?hex_map@dht@@3UHexMap@1@B) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl dht::operator<<<20>(class std::basic_ostream<char,struct std::char_traits<char> > &,class dht::Hash<20> const &)" (??$?6$0BE@@dht@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBV?$Hash@$0BE@@0@@Z) [D:\projects\opendht\build\tools\dhtnode.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol "public: static struct dht::ValueType const dht::ValueType::USER_DATA" (?USER_DATA@ValueType@dht@@2U12@B) referenced in function "class std::shared_ptr<struct dht::Value> __cdecl std::make_shared<struct dht::Value>(void)" (??$make_shared@UValue@dht@@$$V@std@@YA?AV?$shared_ptr@UValue@dht@@@0@XZ) [D:\projects\opendht\build\tools\dhtnode.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol "public: static struct dht::ValueType const dht::IpServiceAnnouncement::TYPE" (?TYPE@IpServiceAnnouncement@dht@@2UValueType@2@B) referenced in function "void __cdecl cmd_loop(class std::shared_ptr<class dht::DhtRunner> &,struct dht_params &)" (?cmd_loop@@YAXAEAV?$shared_ptr@VDhtRunner@dht@@@std@@AEAUdht_params@@@Z) [D:\projects\opendht\build\tools\dhtnode.vcxproj]
[build] dhtnode.obj : error LNK2019: unresolved external symbol optarg_a referenced in function "struct dht_params __cdecl parseArgs(int,char * *)" (?parseArgs@@YA?AUdht_params@@HPEAPEAD@Z) [D:\projects\opendht\build\tools\dhtnode.vcxproj]

Problems with getopt_long_a, optarg_a and __imp_htons are solved by including wingetopt.c to the tools targets

# if (OPENDHT_STATIC)
    if (MSVC)
        set (MSC_COMPAT_SOURCES ${MSC_COMPAT_DIR}/wingetopt.c)
    endif ()
# endif ()

The rest errors occur due to incorrect export of static members

You must provide the definitions of all such members in the same program. Otherwise, a linker error is generated.

@novemus
Copy link
Author

novemus commented Jul 12, 2024

In general, it seems that the code needs to be refactored for class export policy. Many of them don't need to be exported, because are fully defined in header files. By the way, the cause of some linking errors is not wrong export as I thought first, but import attribute is incorrect when linking dll library. Below is another way to fix windows dynamic build
diff.txt
But I haven't tested this in all configurations yet.

@novemus
Copy link
Author

novemus commented Jul 13, 2024

It seems that the changes are more correct now. API preserves backward compatibility. But, in my opinion, the export of classes still needs to be revised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants