用Clang ++构建共享库 [英] Build shared library with Clang++

查看:172
本文介绍了用Clang ++构建共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Clang ++构建共享库(适用于Windows的dll)。

I am trying to build a shared library (dll for Windows) using Clang++.

我已经运行了以下命令:

I have run the following commands:

clang++ -c -o hello.o hello.cpp
clang++ -shared -v -o hello.dll hello.o

第一个命令运行正常,但是当我尝试生成dll时出现此错误:

The first command works fine but when I try to build the dll I get this error:

clang version 3.2 (tags/RELEASE_32/final)
Target: i686-pc-mingw32
Thread model: posix
 "c:/MinGW/bin/g++.exe" -shared -v -m32 -o worker.dll worker.o
Using built-in specs.
COLLECT_GCC=c:/MinGW/bin/g++.exe
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.6.2/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../;/mingw/lib/
COLLECT_GCC_OPTIONS='-shared' '-v' '-m32' '-o' 'worker.dll' '-shared-libgcc' '-mtune=i386' '-march=i386'
 c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/collect2.exe --shared -Bdynamic -e _DllMainCRTStartup@12 --enable-auto-image-base -u ___register_frame_info -u ___deregister_frame_info -o worker.dll c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../dllcrt2.o c:/mingw/bin/../lib/gcc/mingw32/4.6.2/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../.. -L/mingw/lib worker.o -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/4.6.2/crtend.o
Cannot export _hello: symbol not found
collect2: ld returned 1 exit status
clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

这是我正在编译的文件:

Here is the file I am compiling:

// hello.cpp
#include <iostream>

#if defined(_WIN32)
  #define LIBRARY_API __declspec(dllexport)
#else
  #define LIBRARY_API
#endif

extern "C" void LIBRARY_API hello();

void hello()
{
  std::cout << "Hello, World!" << std::endl;
}


推荐答案

此重要操作的后续步骤问题是,从clang ++ 6.0版开始,通过clang编译DLL很好。对于MSVC和CLang,C ++中C ++函数的名称处理存在差异,这可能会损害DLL在混合环境中的使用,但这些差异很容易发现和纠正。

A follow up to this important question is that as of clang++ version 6.0, the compilation of a DLL by clang is fine. There are differences for the name mangling of C++ functions in C++ for MSVC and CLang, that can impair the use of DLL's in mixed environments but these differences are easy to spot and correct.

这篇关于用Clang ++构建共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆