函数声明和未解析的外部
我正在照顾一个巨大的旧C程序并将其转换为C++(我是新手)。
由于程序必须在许多不同的平台上以许多不同的配置运行,因此存在大量复杂的预处理器黑客攻击。
在一个文件(称为 file1.c
)中,我调用 functionA()
。
在另一个文件(称为 file2.c
)中,我有一个 functionA()
的定义。
不幸的是,函数的确切类型是由以多种令人眼花缭乱的方式创建的宏集合指定的。
现在链接器抱怨:
functionA 是一个未解析的外部符号。
我怀疑问题在于 file1.c 中看到的原型与 file2.c 中看到的函数的真实定义略有不同。
由于 _cdecl
和 fastcall
之间以及使用和不使用 __forceinline
之间的不匹配,存在很大的细微差异。
有没有某种方法可以准确显示编译器认为 file1.c
所看到的 functionA()
的类型,而不是 >file2.c
?
I am looking after a huge old C program and converting it to C++ (which I'm new to).
There are a great many complicated preprocessor hacks going on connected to the fact that the program must run on many different platforms in many different configurations.
In one file (call it file1.c
) I am calling functionA()
.
And in another file (call it file2.c
) I have a definition of functionA()
.
Unfortunately the exact type of the function is specified by a collection of macros created in a bewildering number of ways.
Now the linker is complaining that:
functionA is an unresolved external symbol.
I suspect that the problem is that the prototype as seen in file1.c is slightly different from the true definition of the function as seen in file2.c.
There is a lot of scope for subtle differences due to mismatches between _cdecl
and fastcall
, and between with and without __forceinline
.
Is there some way to show exactly what the compiler thinks is the type of functionA()
as seen by file1.c
as opposed to file2.c
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将一个标志传递给编译器(我认为是/P),使其输出传递给编译器的完整预处理输出 - 然后您可以打开这个(巨大的)文件,并搜索它和您需要的信息会在那里,某个地方。
You can pass a flag to the compiler (/P, I think) that causes it to output the complete preprocessed output that is passed to the compiler - you can then open this (huge) file, and search through it and the information you need will be in there, somewhere.
您必须将所有现有的 C 代码真正转换为 C++ 吗?这可能需要大量工作,特别是考虑到您到目前为止所描述的内容。
相反,您可以用 C++ 编写新代码并使用
extern "C"
调用 C 代码。例如,在 C++ 源文件中,您可以:这会更改链接,以便 C++ 编译器生成对 C 代码的外部引用,而无需名称修改,从而允许链接器匹配所有内容。
Must you actually convert all the existing C code to C++? This is likely to be a lot of work, especially given what you've described so far.
Instead, you can write new code in C++ and call into the C code using
extern "C"
. For example, in a C++ source file you can:This changes the linkage so the C++ compiler generates external references to the C code without name mangling, allowing the linker to match everything up.
通常,您应该在输出中包含预期的签名和实际的签名。
否则,您可以指示编译器将预处理结果输出到单独的文件中,对于 MSVC 为
cl.exe /p
,对于 gccgcc -E
。Normally you should have the expected and the actual signature in the output.
Otherwise you can instruct the compiler to output the results of the preprocessing into a seperate file,
cl.exe /p
for MSVC and for gccgcc -E
.