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

Building Polyscope as a shared library on Windows #330

Open
P4T0U opened this issue Feb 4, 2025 · 2 comments
Open

Building Polyscope as a shared library on Windows #330

P4T0U opened this issue Feb 4, 2025 · 2 comments

Comments

@P4T0U
Copy link

P4T0U commented Feb 4, 2025

On Windows, I managed to build polyscope as a shared library with a couple of changes:

  1. Export all symbols for polyscope
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -374,4 +374,7 @@
 set_target_properties(polyscope PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
 add_definitions(-DNOMINMAX)
+set_target_properties(polyscope PROPERTIES
+  WINDOWS_EXPORT_ALL_SYMBOLS ON
+)
 
 # Include settings
  1. Force imgui as static. Again, it would search for a .lib file but no symbols are exported.
--- a/deps/imgui/CMakeLists.txt
+++ b/deps/imgui/CMakeLists.txt
@@ -63,5 +63,5 @@
 
   add_library(
-          imgui
+          imgui STATIC
           ${SRCS}
           )

Alternative: Export all symbols like 1.

  1. Force stb as static. Otherwise, it would search for a non-existent .lib file because no symbols are exported.
--- a/deps/stb/CMakeLists.txt
+++ b/deps/stb/CMakeLists.txt
@@ -1,5 +1,5 @@
 # Create a library for the viewer code
 add_library(
-    stb
+    stb STATIC
     stb_impl.cpp
 )

I tried exporting all symbols but it did not work. I would get missing symbols:

[build] screenshot.cpp.obj : error LNK2001: unresolved external symbol "int stbi_write_png_compression_level" (?stbi_write_png_compression_level@@3HA)
  1. Tell glm to not build a library. It is header-only anyway:
set(GLM_BUILD_LIBRARY OFF CACHE BOOL "Use GLM as header-only" FORCE)

It is not optimal to export all symbols on Windows, but it works. Dropping this here for others to use.

Open to make a PR but I would rather figure out the approved method first 😄

@T7imal
Copy link

T7imal commented Feb 7, 2025

https://cmake.org/cmake/help/latest/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html
For global data symbols, __declspec(dllimport) must still be used when compiling against the code in the .dll.

So you still need to explicitly export global variables.

@P4T0U
Copy link
Author

P4T0U commented Feb 7, 2025

https://cmake.org/cmake/help/latest/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html
For global data symbols, __declspec(dllimport) must still be used when compiling against the code in the .dll.

So you still need to explicitly export global variables.

Thank you! You are right. Because of that, I wasn't able to specify a user Callback. I had to create a export.h:

#ifndef POLYSCOPE_EXPORT_H
#define POLYSCOPE_EXPORT_H

#ifdef _WIN32
  #ifdef POLYSCOPE_DLL_EXPORTS
    #define POLYSCOPE_API __declspec(dllexport)
  #else
    #define POLYSCOPE_API __declspec(dllimport)
  #endif
#else
  #define POLYSCOPE_API
#endif

#endif // POLYSCOPE_EXPORT_H

and in polyscope.h, add it to the declaration like follows:

POLYSCOPE_API extern std::function<void()>& userCallback;

then finally, add POLYSCOPE_DLL_EXPORTS to the definition:

target_compile_definitions(polyscope PRIVATE POLYSCOPE_DLL_EXPORTS)

I'm still a beginner with Polyscope so I'm not sure what other variables need exporting.

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

No branches or pull requests

2 participants