eclipse c/c++项目,GCC编译G++代码
eclipse c/c++ project, GCC compiling G++ code
我使用系统 workbench 4 stm32 (eclipse)。
我有一个混合的 C/C++ 项目,其中 C++ 文件包含 C(从来没有问题),C 文件包含 C++ 文件(大麻烦)。
我一直清楚地为 C++ 文件定义 .cpp/.hpp,为 C 文件定义 .c/.h。此外,如果 __cpluplus extern "C".
任何 C 文件都已声明
但是 GCC 编译,然后导致一个 c++ 头文件 (.hpp) 然后会用 GCC 编译并告诉我 "unknown type class"... 但据我所知,这不应该发生,因为它声明了 c++ ( .hpp)
我如何处理这个项目:
选项A:
以某种方式删除项目的 GCC 编译器并仅使用 G++
问题A1:如何从项目中删除一个编译器?
问题 A2:g++ 比较挑剔,不会全部编译,因为我使用 freertos 例如它做了很多我的编译器根本不喜欢的类型转换
选项B:
具有严格的顶级架构,其中允许 c++ 包含 c 文件,但不允许包含其他方式(这也不是一个好的选择,因为我使用需要进行 callbacks/isr 处理的 stm32 hal 层(c 到然后我的代码是c++)
没有选项:
仅使用 gcc
你是怎么解决这些问题的?
多谢
会为此疯狂:-)
a GCC compilation, then leading to a c++ header (.hpp) then would
compile with GCC and tell my "unknown type class"... but as far as i
know this shouldnt happen since its declaired c++ (.hpp)
您可以在 .c
或 .cpp
文件中包含 任何内容,它将被编译为包含文件的一部分,使用相同的语言,就好像它是它的一部分。以 Qt headers 为例,它们被命名为 QWidget
、QString
,根本没有扩展名。它们又包括 qwidget.h
和 qstring.h
,它们不是有效的 C 文件,其中定义了很多 classes。我在我现在正在进行的项目中这样做:
uint8_t foo[] = {
#include "bar.txt"
};
其中 bar.txt
只是一个数字列表。编译器不介意扩展名,或者它本身不是有效的 C 或 C++ 文件。
您可以使用 #ifdef __cplusplus
屏蔽 C++ headers 中与 C 不兼容的结构,例如
#ifdef __cplusplus
int foo(int);
int foo(int, int);
class example {
public:
example(int);
int field;
};
extern "C" {
#endif
int bar(int);
#ifdef __cplusplus
}
#endif
foo()
被重载,所以它在 C 中不可用,并且不能进入 extern "C"
块,因此它在 #ifdef __cplusplus
中声明以隐藏它 gcc
. class 声明同样受到保护。 bar()
可以在 .cpp
文件中定义,然后可以从 C 代码调用它,它可以与任何 C++ 结构一起使用,例如 classes、重载函数等。
我使用系统 workbench 4 stm32 (eclipse)。 我有一个混合的 C/C++ 项目,其中 C++ 文件包含 C(从来没有问题),C 文件包含 C++ 文件(大麻烦)。
我一直清楚地为 C++ 文件定义 .cpp/.hpp,为 C 文件定义 .c/.h。此外,如果 __cpluplus extern "C".
任何 C 文件都已声明但是 GCC 编译,然后导致一个 c++ 头文件 (.hpp) 然后会用 GCC 编译并告诉我 "unknown type class"... 但据我所知,这不应该发生,因为它声明了 c++ ( .hpp)
我如何处理这个项目:
选项A: 以某种方式删除项目的 GCC 编译器并仅使用 G++
问题A1:如何从项目中删除一个编译器? 问题 A2:g++ 比较挑剔,不会全部编译,因为我使用 freertos 例如它做了很多我的编译器根本不喜欢的类型转换
选项B: 具有严格的顶级架构,其中允许 c++ 包含 c 文件,但不允许包含其他方式(这也不是一个好的选择,因为我使用需要进行 callbacks/isr 处理的 stm32 hal 层(c 到然后我的代码是c++)
没有选项: 仅使用 gcc
你是怎么解决这些问题的? 多谢 会为此疯狂:-)
a GCC compilation, then leading to a c++ header (.hpp) then would compile with GCC and tell my "unknown type class"... but as far as i know this shouldnt happen since its declaired c++ (.hpp)
您可以在 .c
或 .cpp
文件中包含 任何内容,它将被编译为包含文件的一部分,使用相同的语言,就好像它是它的一部分。以 Qt headers 为例,它们被命名为 QWidget
、QString
,根本没有扩展名。它们又包括 qwidget.h
和 qstring.h
,它们不是有效的 C 文件,其中定义了很多 classes。我在我现在正在进行的项目中这样做:
uint8_t foo[] = {
#include "bar.txt"
};
其中 bar.txt
只是一个数字列表。编译器不介意扩展名,或者它本身不是有效的 C 或 C++ 文件。
您可以使用 #ifdef __cplusplus
屏蔽 C++ headers 中与 C 不兼容的结构,例如
#ifdef __cplusplus
int foo(int);
int foo(int, int);
class example {
public:
example(int);
int field;
};
extern "C" {
#endif
int bar(int);
#ifdef __cplusplus
}
#endif
foo()
被重载,所以它在 C 中不可用,并且不能进入 extern "C"
块,因此它在 #ifdef __cplusplus
中声明以隐藏它 gcc
. class 声明同样受到保护。 bar()
可以在 .cpp
文件中定义,然后可以从 C 代码调用它,它可以与任何 C++ 结构一起使用,例如 classes、重载函数等。