在 linux 上交叉编译 windows 不会输出 DLL 文件
Cross-compile on linux for windows won't output DLL file
在 linux 上为 windows 编译,这样它会输出一个 windows DLL fuil,它可以 运行 在 windows 上本地运行(不需要 cygwin或类似的东西)。基本上就像我 运行 在 windows 盒子上编译一样,我宁愿避免。
我尝试了很多东西,其中大部分似乎都能成功编译,给我留下 *.la 或 *.so 文件,而不是 *.dll。
我的 configure.ac 文件:
AC_INIT([libaparapi], [1.4.2], [syncleus@syncleus.com])
AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
LT_INIT
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([subdir-objects])
AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
AC_CANONICAL_HOST
case $host_os in
darwin* )
AC_LDFLAGS="-framework OpenCL -version-info 5:2:4"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I${JAVA_HOME}/include/darwin -I${JAVA_HOME}/include -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS"
;;
mingw*)
AC_LDFLAGS="-no-undefined -lOpenCL -version-info 5:2:4 -no-undefined"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-openjdk/include -I/usr/lib/jvm/java-8-openjdk/include/linux -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS -fpermissive"
;;
*)
AC_LDFLAGS="-lOpenCL -version-info 5:2:4 -no-undefined"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I/usr/lib/jvm/default/include -I/usr/lib/jvm/default/include/linux -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux -I/usr/lib/jvm/java-8-openjdk/include -I/usr/lib/jvm/java-8-openjdk/include/linux -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS -fpermissive"
;;
esac
AC_SUBST(AC_LDFLAGS)
AC_SUBST(AC_CPPFLAGS)
AC_OUTPUT
在上面的案例中,似乎是 *)
案例被激发,ming*)
案例可以忽略,它是先前尝试失败的一部分,这里没有调用。
我的makefile.am:
ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = foreign
EXTRA_DIST = include src/cpp/CLHelper.h src/cpp/classtools.h src/cpp/invoke/JavaArgs.h src/cpp/invoke/OpenCLMem.h src/cpp/invoke/OpenCLKernel.h src/cpp/invoke/OpenCLJNI.h src/cpp/invoke/OpenCLArgDescriptor.h src/cpp/invoke/OpenCLProgram.h src/cpp/CLException.h src/cpp/JNIHelper.h src/cpp/Common.h src/cpp/runKernel/KernelArg.h src/cpp/runKernel/Range.h src/cpp/runKernel/ProfileInfo.h src/cpp/runKernel/AparapiBuffer.h src/cpp/runKernel/Config.h src/cpp/runKernel/Aparapi.h src/cpp/runKernel/ArrayBuffer.h src/cpp/runKernel/JNIContext.h src/cpp/runKernel/List.h
lib_LTLIBRARIES = libaparapi.la
libaparapi_la_CPPFLAGS = $(AC_CPPFLAGS)
libaparapi_la_LDFLAGS = $(AC_LDFLAGS)
libaparapi_la_SOURCES = src/cpp/runKernel/Aparapi.cpp src/cpp/runKernel/ArrayBuffer.cpp src/cpp/runKernel/AparapiBuffer.cpp src/cpp/runKernel/Config.cpp src/cpp/runKernel/JNIContext.cpp src/cpp/runKernel/KernelArg.cpp src/cpp/runKernel/ProfileInfo.cpp src/cpp/runKernel/Range.cpp src/cpp/invoke/OpenCLJNI.cpp src/cpp/invoke/OpenCLArgDescriptor.cpp src/cpp/invoke/OpenCLMem.cpp src/cpp/CLHelper.cpp src/cpp/classtools.cpp src/cpp/JNIHelper.cpp src/cpp/agent.cpp
all-local:
我尝试使用 dockcross,但遇到了与本地使用时相同的问题,因此我将分享我调用的本地编译命令。正如您在下面看到的,我安装了 clang 进行交叉编译,因为有人告诉我这是获得真正本机 DLL 的唯一方法,像 mingw 这样的东西需要静态打包大多数库才能工作。
git clean -xdf
cp -r ../include/ .
mkdir -p m4
aclocal --force
libtoolize --force --copy
automake --foreign --add-missing --copy --force
autoconf --force
CC=clang ./configure --target=windows-shared-x64
CC=clang make && ls .libs
当运行以下是配置阶段的输出时,运行执行上述命令时不会产生错误,因此这是唯一相关的花絮。
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for gcc... clang
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether clang accepts -g... yes
checking for clang option to accept ISO C89... none needed
checking whether clang understands -c and -o together... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by clang... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... gawk
checking command to parse /usr/bin/nm -B output from clang object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking how to run the C preprocessor... clang -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if clang supports -fno-rtti -fno-exceptions... yes
checking for clang option to produce PIC... -fPIC -DPIC
checking if clang PIC flag -fPIC -DPIC works... yes
checking if clang static flag -static works... yes
checking if clang supports -c -o file.o... yes
checking if clang supports -c -o file.o... (cached) yes
checking whether the clang linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking whether make sets $(MAKE)... yes
checking whether make supports the include directive... yes (GNU style)
checking whether make supports nested variables... yes
checking dependency style of clang... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking dependency style of g++... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing libtool commands
config.status: executing depfiles commands
最后的 ls 命令清楚地显示没有生成 DLL:
libaparapi.la libaparapi.lai libaparapi.so libaparapi.so.1 libaparapi.so.1.4.2
知道我做错了什么吗?我对交叉编译很陌生。
所以,您似乎将目标设置为 windows,而不是主机,因此所有内容都设置为在 Linux 上托管,因此生成了 .so 文件。目标三元组证明 --target 看起来也不正确。执行 configure --host=x86_64-w64-mingw32
可能会解决问题。
如果允许使用未定义的符号,GCC 将无法构建 Windows DLL。
您通常可以通过在 ./configure
行的末尾添加 LDFLAGS="-Wl,-no-undefined"
来解决此问题。
但我已经看到一些情况下这没有帮助,但是 运行 ./configure
之后的这一行修复了它:
sed -i.bak -e "s/\(allow_undefined=\)yes/no/" libtool
然后是导入部分,您实际上必须将函数声明为 dllexport
。一种方法是在 header 文件的顶部添加类似的内容:
# if defined(_WIN32) && (defined(BUILD_MYLIB_DLL) || defined (MyLib_EXPORT))
# define DLL_EXPORT_MYLIB __declspec(dllexport)
# elif defined(_WIN32) && !defined(STATIC) && !defined(BUILD_MYLIB_STATIC) && !defined(BUILD_MYLIB)
# define DLL_EXPORT_MYLIB __declspec(dllimport)
# else
# define DLL_EXPORT_MYLIB
# endif
#endif
然后在你需要从 DLL 中获得的每个函数的定义之前添加 DLL_EXPORT_MYLIB
。例如:
DLL_EXPORT_MYLIB const char* mylib_get_version ();
由于上面的 header 代码仅在定义 BUILD_MYLIB_DLL
时导出,您还需要在末尾添加 CFLAGS="-DBUILD_MYLIB_DLL"
and/or CXXFLAGS="-DBUILD_MYLIB_DLL"
./configure
行。
请注意,还有其他方法(例如使用 .def
文件)来构建 DLL,但我发现这是最干净、最便携的方法,尤其是在使用 autoconf 工具时。
您考虑过使用 CMake 吗?
使用 CMake 时更容易一些。只需将 SHARED
添加到 add_library()
命令即可。不过,仍然需要上面的 header 修改。
例如 CMakeLists.txt 中的这一行:
add_library(MyLib SHARED mylib.c)
将自动尝试构建 DLL 并在 C/C++ 中定义 MyLib_EXPORT
,以便可以在 header 文件中检测到它以使用 dllexport。
在 linux 上为 windows 编译,这样它会输出一个 windows DLL fuil,它可以 运行 在 windows 上本地运行(不需要 cygwin或类似的东西)。基本上就像我 运行 在 windows 盒子上编译一样,我宁愿避免。
我尝试了很多东西,其中大部分似乎都能成功编译,给我留下 *.la 或 *.so 文件,而不是 *.dll。
我的 configure.ac 文件:
AC_INIT([libaparapi], [1.4.2], [syncleus@syncleus.com])
AC_ENABLE_SHARED(yes)
AC_ENABLE_STATIC(no)
LT_INIT
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([subdir-objects])
AC_PROG_CXX
AC_CONFIG_FILES([Makefile])
AC_CANONICAL_HOST
case $host_os in
darwin* )
AC_LDFLAGS="-framework OpenCL -version-info 5:2:4"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I${JAVA_HOME}/include/darwin -I${JAVA_HOME}/include -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS"
;;
mingw*)
AC_LDFLAGS="-no-undefined -lOpenCL -version-info 5:2:4 -no-undefined"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-openjdk/include -I/usr/lib/jvm/java-8-openjdk/include/linux -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS -fpermissive"
;;
*)
AC_LDFLAGS="-lOpenCL -version-info 5:2:4 -no-undefined"
AC_CPPFLAGS="-Iinclude -I/opt/AMDAPP/SDK/include -Isrc/cpp -Isrc/cpp/runKernel -Isrc/cpp/invoke -I/usr/lib/jvm/default/include -I/usr/lib/jvm/default/include/linux -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux -I/usr/lib/jvm/java-8-openjdk/include -I/usr/lib/jvm/java-8-openjdk/include/linux -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ -DCL_USE_DEPRECATED_OPENCL_1_1_APIS -fpermissive"
;;
esac
AC_SUBST(AC_LDFLAGS)
AC_SUBST(AC_CPPFLAGS)
AC_OUTPUT
在上面的案例中,似乎是 *)
案例被激发,ming*)
案例可以忽略,它是先前尝试失败的一部分,这里没有调用。
我的makefile.am:
ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = foreign
EXTRA_DIST = include src/cpp/CLHelper.h src/cpp/classtools.h src/cpp/invoke/JavaArgs.h src/cpp/invoke/OpenCLMem.h src/cpp/invoke/OpenCLKernel.h src/cpp/invoke/OpenCLJNI.h src/cpp/invoke/OpenCLArgDescriptor.h src/cpp/invoke/OpenCLProgram.h src/cpp/CLException.h src/cpp/JNIHelper.h src/cpp/Common.h src/cpp/runKernel/KernelArg.h src/cpp/runKernel/Range.h src/cpp/runKernel/ProfileInfo.h src/cpp/runKernel/AparapiBuffer.h src/cpp/runKernel/Config.h src/cpp/runKernel/Aparapi.h src/cpp/runKernel/ArrayBuffer.h src/cpp/runKernel/JNIContext.h src/cpp/runKernel/List.h
lib_LTLIBRARIES = libaparapi.la
libaparapi_la_CPPFLAGS = $(AC_CPPFLAGS)
libaparapi_la_LDFLAGS = $(AC_LDFLAGS)
libaparapi_la_SOURCES = src/cpp/runKernel/Aparapi.cpp src/cpp/runKernel/ArrayBuffer.cpp src/cpp/runKernel/AparapiBuffer.cpp src/cpp/runKernel/Config.cpp src/cpp/runKernel/JNIContext.cpp src/cpp/runKernel/KernelArg.cpp src/cpp/runKernel/ProfileInfo.cpp src/cpp/runKernel/Range.cpp src/cpp/invoke/OpenCLJNI.cpp src/cpp/invoke/OpenCLArgDescriptor.cpp src/cpp/invoke/OpenCLMem.cpp src/cpp/CLHelper.cpp src/cpp/classtools.cpp src/cpp/JNIHelper.cpp src/cpp/agent.cpp
all-local:
我尝试使用 dockcross,但遇到了与本地使用时相同的问题,因此我将分享我调用的本地编译命令。正如您在下面看到的,我安装了 clang 进行交叉编译,因为有人告诉我这是获得真正本机 DLL 的唯一方法,像 mingw 这样的东西需要静态打包大多数库才能工作。
git clean -xdf
cp -r ../include/ .
mkdir -p m4
aclocal --force
libtoolize --force --copy
automake --foreign --add-missing --copy --force
autoconf --force
CC=clang ./configure --target=windows-shared-x64
CC=clang make && ls .libs
当运行以下是配置阶段的输出时,运行执行上述命令时不会产生错误,因此这是唯一相关的花絮。
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for gcc... clang
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether clang accepts -g... yes
checking for clang option to accept ISO C89... none needed
checking whether clang understands -c and -o together... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for fgrep... /usr/bin/grep -F
checking for ld used by clang... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... gawk
checking command to parse /usr/bin/nm -B output from clang object... ok
checking for sysroot... no
checking for a working dd... /usr/bin/dd
checking how to truncate binary pipes... /usr/bin/dd bs=4096 count=1
checking for mt... no
checking if : is a manifest tool... no
checking how to run the C preprocessor... clang -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if clang supports -fno-rtti -fno-exceptions... yes
checking for clang option to produce PIC... -fPIC -DPIC
checking if clang PIC flag -fPIC -DPIC works... yes
checking if clang static flag -static works... yes
checking if clang supports -c -o file.o... yes
checking if clang supports -c -o file.o... (cached) yes
checking whether the clang linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking whether make sets $(MAKE)... yes
checking whether make supports the include directive... yes (GNU style)
checking whether make supports nested variables... yes
checking dependency style of clang... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking for ld used by g++... /usr/bin/ld -m elf_x86_64
checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking for g++ option to produce PIC... -fPIC -DPIC
checking if g++ PIC flag -fPIC -DPIC works... yes
checking if g++ static flag -static works... yes
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking dependency style of g++... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing libtool commands
config.status: executing depfiles commands
最后的 ls 命令清楚地显示没有生成 DLL:
libaparapi.la libaparapi.lai libaparapi.so libaparapi.so.1 libaparapi.so.1.4.2
知道我做错了什么吗?我对交叉编译很陌生。
所以,您似乎将目标设置为 windows,而不是主机,因此所有内容都设置为在 Linux 上托管,因此生成了 .so 文件。目标三元组证明 --target 看起来也不正确。执行 configure --host=x86_64-w64-mingw32
可能会解决问题。
如果允许使用未定义的符号,GCC 将无法构建 Windows DLL。
您通常可以通过在 ./configure
行的末尾添加 LDFLAGS="-Wl,-no-undefined"
来解决此问题。
但我已经看到一些情况下这没有帮助,但是 运行 ./configure
之后的这一行修复了它:
sed -i.bak -e "s/\(allow_undefined=\)yes/no/" libtool
然后是导入部分,您实际上必须将函数声明为 dllexport
。一种方法是在 header 文件的顶部添加类似的内容:
# if defined(_WIN32) && (defined(BUILD_MYLIB_DLL) || defined (MyLib_EXPORT))
# define DLL_EXPORT_MYLIB __declspec(dllexport)
# elif defined(_WIN32) && !defined(STATIC) && !defined(BUILD_MYLIB_STATIC) && !defined(BUILD_MYLIB)
# define DLL_EXPORT_MYLIB __declspec(dllimport)
# else
# define DLL_EXPORT_MYLIB
# endif
#endif
然后在你需要从 DLL 中获得的每个函数的定义之前添加 DLL_EXPORT_MYLIB
。例如:
DLL_EXPORT_MYLIB const char* mylib_get_version ();
由于上面的 header 代码仅在定义 BUILD_MYLIB_DLL
时导出,您还需要在末尾添加 CFLAGS="-DBUILD_MYLIB_DLL"
and/or CXXFLAGS="-DBUILD_MYLIB_DLL"
./configure
行。
请注意,还有其他方法(例如使用 .def
文件)来构建 DLL,但我发现这是最干净、最便携的方法,尤其是在使用 autoconf 工具时。
您考虑过使用 CMake 吗?
使用 CMake 时更容易一些。只需将 SHARED
添加到 add_library()
命令即可。不过,仍然需要上面的 header 修改。
例如 CMakeLists.txt 中的这一行:
add_library(MyLib SHARED mylib.c)
将自动尝试构建 DLL 并在 C/C++ 中定义 MyLib_EXPORT
,以便可以在 header 文件中检测到它以使用 dllexport。