Re: Question about static and shared libraries and their usage in a binary on Windows in a autotools project
Roumen Petrov <[email protected]>
| Newsgroups | gmane.comp.gnu.libtool.general |
|---|---|
| Message-ID | <[email protected]> |
Hi Vincent,
Sorry for top posting.
Perhaps is not easy visible for manual ( https://www.gnu.org/software/libtool/manual/html_node/Creating-object-files.html ) use of conditional code.
In this case #ifdef PIC.
Perhaps better solution is use of -export-symbols.
Vincent Torri wrote:
> 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
>
Regards,
Roumen Petrov