[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Question about static and shared libraries and their usage in a binary o
From: |
Vincent Torri |
Subject: |
Question about static and shared libraries and their usage in a binary on Windows in a autotools project |
Date: |
Tue, 10 Aug 2021 05:16:48 +0200 |
Hello
I contribute to an autotools project. The tree is :
src/lib/libfoo <--- the library, with libfoo.h declaring the public symbols
src/bin/bar <-- the binary which uses libfoo and includes libfoo.h in
its source files
I want to create the library 'libfoo' on Windows. I also want to
declare public symbols with __declspec(dllexport) and
__declspec(dllimport) in a macro that I call MY_API in libfoo.h
Thanks to DLL_EXPORT that libtool is passing to the preprocessor when
compiling the library 'libfoo', there is no problem to compile libfoo.
MY_API is correctly defined either when I build the static lib, or the
shared lib, or both.
The problem I am facing is when I build the 'bar' binary. On Windows:
1) if 'lifoo' is built as a shared library, when I include libfoo.h in
'bar' source files, MY_API must be defined as __declspec(dllimport)
2) if 'libfoo' is built as a static library, when I include libfoo.h
in 'bar' source files, MY_API must be defined as nothing
but, as far as I know, when I compile 'bar', I couldn't find a way to
know if 'libfoo' has been compiled as a static library or as a shared
library. I have looked at the 'bar' source files gcc calls, and they
are the same in both cases (libfoo compiled as a static or shared
lib). So I don't know how I can correctly define my macro MY_API.
Here is, for now, my macro:
#if defined(_WIN32) || defined(__CYGWIN__)
# ifdef FOO_BUILD // defined when building 'libfoo'
# ifdef DLL_EXPORT
# warning "BUILD DLL"
# define MY_API __declspec(dllexport)
# else
# warning "BUILD STATIC"
# define MY_API
# endif
# else
# warning "IMPORT DLL"
# define MY_API __declspec(dllimport)
# endif
in the last #else, I don't know what to do to correctly manage MY_API
for my problem above
One solution would be : never compile 'lbfoo' as a static lib ("DLL
are good on Windows"), but I would like to support both static and
shared libraries.
Does someone know how to solve my issue ? (I hope I've been clear enough...)
thank you
Vincent Torri
- Question about static and shared libraries and their usage in a binary on Windows in a autotools project,
Vincent Torri <=
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Roumen Petrov, 2021/08/10
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Vincent Torri, 2021/08/10
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Nick Bowler, 2021/08/10
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Vincent Torri, 2021/08/10
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Bob Friesenhahn, 2021/08/11
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Alexei Podtelezhnikov, 2021/08/11
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Bob Friesenhahn, 2021/08/11
- Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project, Vincent Torri, 2021/08/11