c++ - 为不同源文件中的函数命名

标签 c++ c

我有一个 .h 文件和三个 .cpp 文件。所有 cpp 文件都包含 .h 文件。 我想在其中一个 cpp 文件中创建一个无用的函数,并在其他 cpp 文件中使用别名来引用这个无用的函数。但如果我将函数从 important.cpp 移至unusable.cpp,它就不想再编译了。由于某种原因,它看不到该函数,即使它在 header 中声明也是如此。

无用的.h

#ifdef __cplusplus
extern "C" {
#endif
    extern void useless(void);
#ifdef __cplusplus
}
#endif

无用的.cpp

#ifdef __cplusplus
extern "C" {
#endif
    void useless(void) {    }
#ifdef __cplusplus
}
#endif

重要.cpp

void important(void) __attribute__((alias("useless")));

错误:“void important()”别名为 undefined symbol “无用” void important(void) 属性((alias("useless")));

最佳答案

你为什么要使用一堆extern "C"东西?

如果它是一个 C++ 项目和一个全局 C++ 函数,那么您确实应该声明它是一个 C 函数。这样做可能会导致 undefined reference 类型错误,因为 C 和 C++ 的符号名称不同。

关于c++ - 为不同源文件中的函数命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28716877/

相关文章:

c++ - 如何在没有明确说明的情况下从 dll 中导出模板类?

c++ - 结构和指针程序错误(结构,C++)

c - MSP430 上的摩尔斯电码输入

c - 双破折号命令行选项而不是 getopt

c - 基于 Unix 的 shell 在退出时关于子进程的默认行为?

c - 为什么 fwrite 将每个字符写这么多次?

c++ - 虚拟内存地址空间不足(Borland C++ Builder 6 程序)

c++ - 在大型应用程序的主体上使用 try catch

c++ - 错误 LNK1112 : module machine type 'x64' conflicts with target machine type 'X86'

c - 查找 int 链表中最常出现的元素的最简单方法