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

Error when building both static and shared libs on Windows with MSVC #454

Closed
adriendelsalle opened this issue Apr 12, 2024 · 4 comments · Fixed by #455
Closed

Error when building both static and shared libs on Windows with MSVC #454

adriendelsalle opened this issue Apr 12, 2024 · 4 comments · Fixed by #455

Comments

@adriendelsalle
Copy link
Contributor

Description

It's currently not possible to build both static and shared versions of libraries on Windows when using the MSVC-based toolchain because both variants produce a .lib file with the exact same output name.

For the shared libs it represents the export file and for static libs, the library itself.
It would be nice to detect such cases and automatically suffix the output name of the static lib with _static to disambiguate. It is also well documented in a conda-forge enhancement proposal.

I'll open a PR to discuss a possible fix!

Steps to reproduce

I'm building the project using a conda environment:

micromamba create -n sundials -y c-compiler cmake ninja
micromamba activate sundials
cmake -LAH -G "Ninja" -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON -S .
ninja -C build

Then you get at the last step:

ninja: error: build.ninja:997: multiple rules generate bin/sundials_core.lib
@balos1
Copy link
Member

balos1 commented Apr 12, 2024

Hi @adriendelsalle, yes this is a known issue. A fix is currently in development. It is similar to your PR #455, but with an option for the user to control what is done. We would appreciate it if you could test the fix out when it is ready.

@adriendelsalle
Copy link
Contributor Author

Sure! Could you share a link when ready for testing? Thanks for the quick reply :)

@adriendelsalle
Copy link
Contributor Author

I also noticed that the SUNDIALS_EXPORT macro seems invalid when building both variants:

In the following code, sundials_core refers to the shared lib when building both static and shared ones;

include(GenerateExportHeader)
generate_export_header(
sundials_core BASE_NAME SUNDIALS
EXPORT_FILE_NAME
"${PROJECT_BINARY_DIR}/include/sundials/sundials_export.h"
)

causing CMake's generate_export_header to generate the following sundials_export.h:

#ifdef SUNDIALS_STATIC_DEFINE
#  define SUNDIALS_EXPORT
#  define SUNDIALS_NO_EXPORT
#else
#  ifndef SUNDIALS_EXPORT
#    ifdef sundials_core_EXPORTS
        /* We are building this library */
#      define SUNDIALS_EXPORT __declspec(dllexport)
#    else
        /* We are using this library */
#      define SUNDIALS_EXPORT __declspec(dllimport)
#    endif
#  endif

but SUNDIALS_STATIC_DEFINE is set as PRIVATE compile definition of static targets:

target_compile_definitions(${obj_target} PRIVATE SUNDIALS_STATIC_DEFINE)

and
target_compile_definitions(${_actual_target_name} PRIVATE SUNDIALS_STATIC_DEFINE)

Causing the SUNDIALS_EXPORT macro to expand to __declspec(dllimport) even for the static target. Using MSVC it leads to this kind of errors: error LNK2019: unresolved external symbol __imp_CVodeSetLinearSolver (see https://stackoverflow.com/a/5159395)

I think SUNDIALS_STATIC_DEFINE should be a PUBLIC compile definition for static libs

balos1 added a commit that referenced this issue May 16, 2024
#455)

Description
---

Disambiguate shared and static libs `.lib` files on Windows using MSVC
Suffix library output name with `_static` when building both static and
shared libs on Windows

Closes #454

---------

Co-authored-by: Markus Mützel <markus.muetzel@gmx.de>
Co-authored-by: Cody Balos <balos1@llnl.gov>
@balos1
Copy link
Member

balos1 commented May 16, 2024

Closed by #455.

@balos1 balos1 closed this as completed May 16, 2024
gardner48 pushed a commit that referenced this issue Jun 20, 2024
#455)

Description
---

Disambiguate shared and static libs `.lib` files on Windows using MSVC
Suffix library output name with `_static` when building both static and
shared libs on Windows

Closes #454

---------

Co-authored-by: Markus Mützel <markus.muetzel@gmx.de>
Co-authored-by: Cody Balos <balos1@llnl.gov>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants