c++ - "-x c"后返回 native 文件处理

标签 c++ c clang

这是对 add “extern C” as a compiler option for a symbol? 的跟进和 mockinterface 的建议。

使用 clang,-x c 允许我将后续文件视为 C 文件。但是,它将所有后续文件视为 C 文件,包括目标文件。例如,以下内容:

/usr/local/bin/clang++ /usr/local/ssl/fips-2.0/lib/fipscanister.o -x c /usr/local/ssl/fips-2.0/lib/fips_premain.c
-Wall -Wextra main.o /usr/local/ssl/lib/libssl.a /usr/local/ssl/lib/libcrypto.a -o main.exe -Wl,--no-demangle -ldl

main.o:1:1: error: expected unqualified-id
<U+007F>ELF<U+0002><U+0001><U+0001><U+0003>
^
main.o:1:9: warning: null character ignored [-Wnull-character]
<U+007F>ELF<U+0002><U+0001><U+0001><U+0003>
                                           ^
main.o:1:10: warning: null character ignored [-Wnull-character]
<U+007F>ELF<U+0002><U+0001><U+0001><U+0003><U+0000>

<U+007F>ELF<U+0002><U+0001><U+0001><U+0003>
^
main.o:1:9: warning: null character ignored [-Wnull-character]
<U+007F>ELF<U+0002><U+0001><U+0001><U+0003>
                                           ^
main.o:1:10: warning: null character ignored [-Wnull-character]
<U+007F>ELF<U+0002><U+0001><U+0001><U+0003><U+0000>
...

根据前面的问题,我需要 -x c fips_premain.c,因为如果 fips_premain.c 是用 C++ 编译器编译的,那么我会得到未解析的符号。 (并且 fips_premain.c 被 FIPS 进程隔离,因此无法更改)。

我尝试使用 -x native(类似于 -march)切换回 native ,但产生了错误。

如何切换回后续文件的“ native ”处理以进行链接?

最佳答案

您需要重新安排编译命令,使 main.o 位于 -x c 之前(因为 -x c 告诉编译器“无论我使用什么扩展名,以下文件都是 C 源文件”——因此 Clang 尝试将 main.o 编译为源文件,但如您所知失败了...)

您可以通过使用 clang -c fips_premain.c 并在链接内容的行中使用 fips_premain.o 来完成此操作。

换句话说,-x c 后跟

  -x language
       Treat subsequent input files as having type language.

我还没有测试过,但是在你的 .c 文件之后执行 -x none 也可能有效 - gcc 手册说如果 Clang 与 gcc 的兼容性正常工作,这应该有效。编辑:可以确认 -x none 做了预期的事情。

关于c++ - "-x c"后返回 native 文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283940/

相关文章:

c++ - 即使我必须在自己的源代码上使用 RTTI,我是否可以使用 clang 来解析 C 代码?

c++ - c++中windows-thread异常输出

c++ - boost::make_zip_iterator 的 decltype?

c - 为什么像 "volatile int * p"这样的指向 volatile 的指针有用?

c++ - 用C++标准库分析Clang线程安全

c++11 统一初始化不适用于 clang++

c++ - C 和 C++ 中 struct 的区别

C++ std::maps 和带有构造函数的类值

java - Android 上的 JNI : How to retrieve a string from Java code?

在外部服务器上编译代码(我没有根访问权限)