I am trying to create a shared library on Windows. I am able to create this shared library on linux but on windows I get linker errors. I am using the MinGW G++ 4.5 compiler. I will first present the source code to the example on linux , and then present the file which I tried to change on windows.
/home/nxd/Progs/C++/shared-lib>cat lib_interface.h
#ifndef LIBINTERFACE_H
#define LIBINTERFACE_H
int func(int a);
#endif /* LIBINTERFACE_H */
/home/nxd/Progs/C++/shared-lib>cat sh_lib.cpp
extern int b;
int func(int a)
{
return a+b;
}
/home/nxd/Progs/C++/shared-lib>cat main.cpp
#include <iostream>
#include "lib_interface.h"
int b;
int main()
{
b=10;
std::cout << func(20) << std::endl;
}
/home/nxd/Progs/C++/shared-lib>cat Makefile
OBJS = main.o sh_lib.o
test: main.o libshared.so
g++ -L. -o$@ main.o -lshared
main.o: main.cpp lib_interface.h
g++ -c -Wall -fPIC $<
sh_lib.o: sh_lib.cpp lib_interface.h
g++ -c -Wall -fPIC $<
libshared.so: sh_lib.o
ld -shared -soname=libshared.so -o libshared.so.1 $<
ln -s libshared.so.1 libshared.so
.PHONY: clean
clean:
rm *.o *.so *.so.1
/home/nxd/Progs/C++/shared-lib>make
g++ -c -Wall -fPIC main.cpp
g++ -c -Wall -fPIC sh_lib.cpp
ld -shared -soname=libshared.so -o libshared.so.1 sh_lib.o
ln -s libshared.so.1 libshared.so
g++ -L. -otest main.o -lshared
/home/nxd/Progs/C++/shared-lib>LD_LIBRARY_PATH=. ./test
30
I tried to make a shared library out of sh_lib.cpp using MinGW g++ on Windows. The reason there are so many comments is because of the many things I tried before I decided to post this.
/home/nxd>cat sh_lib.cpp
//extern "C" __declspec(dllimport) int b;
//#ifdef __cplusplus
//extern "C" {
//#endif
__declspec(dllimport) extern int b;
//__declspec(dllimport) int b;
//extern int b;
//#ifdef __cplusplus
//}
//#endif
int func(int a)
{
return a+b;
}
Here is the relevant portion of the Makefile with various tweaks. The options passed to the linker that you see below were added one by one to try to get rid of the link error. The ld command below was just to be sure that the linker was indeed getting the options. The preceding hyphen tells make to run the next command even if the current one had errors.
sh_lib.o: sh_lib.cpp lib_interface.h
g++ -c -Wall -fPIC $<
libshared.so: sh_lib.o
-ld -shared --enable-auto-import --unresolved-symbols=ignore-in-shared-libs -soname=libshared.so --allow-shlib-undefined -o libshared.so.1 $<
g++ -shared -Wl,--unresolved-symbols=ignore-in-shared-libs -Wl,--enable-auto-import -Wl,--allow-shlib-undefined -Wl,-soname,libshared.so -o libshared.so.1 $<
My Question: how can I compile sh_lib.cpp into a dll using the extern variable like the way it works in linux?. I did read the stackoverflow article : http://stackoverflow.com/questions/56500/cant-access-variable-in-c-dll-from-a-c-app , but "I think" there is a subtle difference here between the 2 cases. There he was trying to link with a file, here I am trying to create a shared library, using a variable for which storage has not been allocated (extern), and I want to tell the linker to ignore this variable's storage when creating the library - it will be resolved when I am linking with an exe. I am currently managing this problem with static linking.
Many thanks for your help in advance.