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

Fix static GLEW linkage #54

Closed
wants to merge 1 commit into from

Conversation

jcowles
Copy link

@jcowles jcowles commented Sep 14, 2016

In glew.h, it is required to define GLEW_STATIC when using the static library,
however USD never sets this flag, thus previously all glew linkage was assumed
to be dynamic with "dllimport" linkage.

Now the CMake FindGlew module sets this flag when running in Win32 and the
static library is discovered (this is determined by convention of the "s" or "sd"
suffix).

Also, GLEW debug libraries are now supported (e.g. glew32[s]d.lib).

Addresses Issue #53

In glew.h, it is required to define GLEW_STATIC when using the static library,
however USD never sets this flag, thus previously all glew linkage was assumed
to be dynamic with "dllimport" linkage.

Now the CMake FindGlew module sets this flag when running in Win32 and the
static library is discovered (this is determined by convention of the "s" or "sd"
suffix).

Also, GLEW debug libraries are now supported (e.g. glew32[s]d.lib).
@jcowles
Copy link
Author

jcowles commented Oct 22, 2016

Don't merge this yet, it needs another tweak to be correct.

@@ -62,6 +62,12 @@ if (WIN32)
PATH_SUFFIXES
Release/${ARCH}
DOC "The GLEW library")

if ("${GLEW_LIBRARY}" MATCHES "glew32s(d|)")
add_definitions("/DGLEW_STATIC")
Copy link
Author

Choose a reason for hiding this comment

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

I believe this should be GLEW_BUILD (not GLEW_STATIC) -- I will confirm and update the PR.

Copy link
Member

Choose a reason for hiding this comment

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

GLEW_BUILD is supposed to be defined when you are building the GLEW library itself IIRC.

BTW, usd-build-club is currently working around this issue by building the glew dll instead of the static lib. Previously, it was creating static glew...

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that's what I thought from the docs too, however Windows has a quirk where global variables declared "extern" will not be shared over library boundaries unless they are declared with dllexport declspec. Looking at the GLEW_STATIC/GLEW_BUILD preprocessor logic, the way to get dllexport is by declaring GLEW_BUILD.

So it seems this is the only way to make it work, unless you are ok with unique glew state per-library, but the USD imaging init pattern requires shared state, since Glf calls glewInit() (via GlfGlewInit); so without shared state, Glf will be the only library that gets initialized correctly.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, the unstated bit in my comment is that glew stores GL functions as global function pointer variables of the form __glewApiName -- e.g. glDrawBuffers is internally stored as a global function pointer called __glewDrawBuffers, thus hitting the scenario I described above.

Copy link
Member

Choose a reason for hiding this comment

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

/*
 * GLEW_STATIC is defined for static library.
 * GLEW_BUILD  is defined for building the DLL library.
 */

#ifdef GLEW_STATIC
#  define GLEWAPI extern
#else
#  ifdef GLEW_BUILD
#    define GLEWAPI extern __declspec(dllexport)
#  else
#    define GLEWAPI extern __declspec(dllimport)
#  endif
#endif

If you don't have GLEW_STATIC, and you do have GLEW_BUILD, USD will attempt to re-export the symbols, so if you use GLEW yourself, and link glf, then the linker will report duplicate symbols? I agree that these macros in glew.h are bogus, extern is seldom what you want in this case.

In Zeno, I worked around this issue by requiring Zeno libs to link the PlatformGL library, and never glew directly. Maybe this hiccough needs to be documented...

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, seems wrong -- and thinking about this more this morning, I believe the real problem is static linkage of Glew into a dynamic shared library. We should really just be linking Glew dynamically, then everything would work as expected... aside from the linker errors I get from dynamic linking when combining USD and OpenSubdiv (but that's just something that needs to be fixed).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's why build-club is using dynamic now. Yesterday, I did a build of Pixar's dev_win_ip with @JamieKenyonFoundry's latest dev_win_ip merged on top, and usd-build-club built GLEW and OpenSubdiv and it all worked :) What link problem are you seeing in OSD?

Copy link
Author

Choose a reason for hiding this comment

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

Linker error (symbol not found for __glewXXX) when building Hd, the error originates from OpenSubdiv Osd classes.

@jcowles
Copy link
Author

jcowles commented Oct 25, 2016

I'm getting opensubdiv related errors (missing__glesXxxxx symbols) when
linking Hd.dll

Was the build working without Jamie's changes?

On Monday, October 24, 2016, Nick Porcino [email protected] wrote:

@meshula commented on this pull request.

In cmake/modules/FindGLEW.cmake
#54:

@@ -62,6 +62,12 @@ if (WIN32)
PATH_SUFFIXES
Release/${ARCH}
DOC "The GLEW library")
+

  • if ("${GLEW_LIBRARY}" MATCHES "glew32s(d|)")
  •    add_definitions("/DGLEW_STATIC")
    

Yeah, that's why build-club is using dynamic now. Yesterday, I did a build
of Pixar's dev_win_ip with @JamieKenyonFoundry
https://github.com/JamieKenyonFoundry's latest dev_win_ip merged on
top, and usd-build-club built GLEW and OpenSubdiv and it all worked :) What
link problem are you seeing in OSD?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#54, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABcK4mde6MgtR5-EVz0BR1tNpIvubgtHks5q3UbmgaJpZM4J9Goe
.

@meshula
Copy link
Member

meshula commented Oct 25, 2016

Hmmm, I had a local change. When I back it out, as shown in the diff below, I get the same failure.

meshula@c698023

This is complicated :\

@jcowles
Copy link
Author

jcowles commented Oct 25, 2016

What was your local change?

@jcowles
Copy link
Author

jcowles commented Oct 25, 2016

Yeah, I wondered about that too, but given the fact that Windows DLLs
won't share global variables across library boundaries without declspec of
dllexport, It seems GLEW_BUILD is required -- unless you are ok with
private glew state per library, but this breaks USD imaging assumptions.

On Saturday, October 22, 2016, Nick Porcino [email protected]
wrote:

@meshula commented on this pull request.

In cmake/modules/FindGLEW.cmake
#54:

@@ -62,6 +62,12 @@ if (WIN32)
PATH_SUFFIXES
Release/${ARCH}
DOC "The GLEW library")
+

  • if ("${GLEW_LIBRARY}" MATCHES "glew32s(d|)")
  •    add_definitions("/DGLEW_STATIC")
    

GLEW_BUILD is supposed to be defined when you are building the GLEW
library itself IIRC.

BTW, usd-build-club is currently working around this issue by building the
glew dll instead of the static lib. Previously, it was creating static
glew...


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#54, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABcK4jweFBr52CENcmWEQ4e7x2Uh4BjLks5q2wSvgaJpZM4J9Goe
.

@ghost
Copy link

ghost commented Oct 27, 2016

I'm seeing this now too!

@jcowles
Copy link
Author

jcowles commented Oct 27, 2016

@JamieKenyonFoundry The OpenSubdiv fix is here: PixarAnimationStudios/OpenSubdiv#905

@TheRealJokerMan
Copy link

TheRealJokerMan commented Oct 28, 2016

Thats awesome! Thanks @jcowles and George!

@sunyab
Copy link
Contributor

sunyab commented Jan 20, 2017

Closing this pull request along with #53, please reopen if this is still an issue. Thanks!

@sunyab sunyab closed this Jan 20, 2017
asluk pushed a commit to NVIDIAGameWorks/USD that referenced this pull request Apr 4, 2020
…mni_plugin) (PixarAnimationStudios#54)

* Work in progress on UsdDeltaBuilder

* Incremental delta tests in test_omni_plugin

* Incremental delta code clean-up
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.

4 participants