带有 HAL 的 C++ STM32 的第一个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35288808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:30:19  来源:igfitidea点击:

First project for STM32 with HAL in C++

c++stm32hal

提问by unitatem

I would like to create a project for STM32 with HAL in C++. Unfortunately, CubeMX supports only projects in C. However, HAL support C++.

我想用 C++ 用 HAL 为 STM32 创建一个项目。不幸的是,CubeMX 仅支持 C 中的项目。但是,HAL 支持 C++。

I have created a project in CubeMX and I was trying to import it to any of the IDEs above but without any success because it is imported as a C project. So it doesn't look like a good solution.

我在 CubeMX 中创建了一个项目,我试图将它导入到上面的任何 IDE,但没有成功,因为它是作为 C 项目导入的。所以这看起来不是一个好的解决方案。

Also, I have tried creating a C++ project in the mentioned IDE and adding to it files from CubeMX. It seems to be more logic but I can't do it in the right way because my configuration is still wrong.

此外,我尝试在提到的 IDE 中创建一个 C++ 项目,并从 CubeMX 添加文件。这似乎更符合逻辑,但我无法以正确的方式做到这一点,因为我的配置仍然是错误的。

Could anyone explain me how should I configure new C++ projects in Eclipse or Atollic TrueSTUDIO in order to use HAL?

谁能解释一下我应该如何在 Eclipse 或 Atollic TrueSTUDIO 中配置新的 C++ 项目以使用 HAL?

回答by Jeroen

Recent versions of CubeMX support System Workbench 4 STM32, which is an Eclipse-based IDE pre-configured to develop for STM32 targets. It has the option to convert a project to C++ (Right-click on the project in the project explorer, then select "Convert to C++").

CubeMX 的最新版本支持 System Workbench 4 STM32,这是一个基于 Eclipse 的 IDE,已预先配置为针对 STM32 目标进行开发。它具有将项目转换为 C++ 的选项(在项目资源管理器中右键单击该项目,然后选择“转换为 C++”)。

Note that I have no direct experience using this method. We use the STM HAL and build our C++ applications on top of that, so this is definitely possible, but our IDE is Keil, so YMMV.

请注意,我没有使用这种方法的直接经验。我们使用 STM HAL 并在此基础上构建我们的 C++ 应用程序,所以这绝对是可能的,但我们的 IDE 是 Keil,所以 YMMV。

回答by Jim Fred

How to convert a STM32CubeMX-generated Atollic Eclipse C project to C++ (3 steps):

如何将 STM32CubeMX 生成的 Atollic Eclipse C 项目转换为 C++(3 个步骤):

  • Copy main.c to main.cpp and 'exclude' main.c from build. Result: Doesn't quite work yet. Although main.cpp might appear in the Eclipse project file list, it doesn't even begin to compile (won't catch obvious syntax errors) and there are linker errors for a missing main().
  • Next, Add org.eclipse.cdt.core.ccnature to the .project file with a text editor, like this... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> See http://www.openstm32.org/forumthread1244. This can be done while Eclipse is running with the project open. Result: main.cpp compiles but it can't find include files because the include paths are wrong. You'd think cppnaturewould have been a better name. Edit: search Eclipse help for "C++ nature" for tips on how to add C++ nature using menu File / New / Other / Convert to C/C++ Make Project.
  • Next, Modify project settings to duplicate pertinent C settings over to C++ settings - as shown here. enter image description hereThe project needs to be closed and re-opened for this change to take effect. Result: builds, links, runs and runs correctly. main.cpp can make calls to HAL c code and HAL callbacks can call functions in .cpp files. An extern "C" modifier may be needed in .cpp files if 1the .cpp side is calling a function on the .c side (modify function prototypes by pre-pending defined extern "C"2.c side side is calling a callback defined in a .cpp (modify the function definition on the .cpp side by pre-pending defined extern "C".
  • 将 main.c 复制到 main.cpp 并从构建中“排除”main.c。结果:还没有完全奏效。尽管 main.cpp 可能出现在 Eclipse 项目文件列表中,但它甚至没有开始编译(不会捕获明显的语法错误)并且存在缺少 main() 的链接器错误。
  • 接下来,添加 org.eclipse.cdt.core。cc性质 .project 文件与文本编辑器,像这样... <natures> <nature>org.eclipse.cdt.core.cnature</nature> <nature>org.eclipse.cdt.core.ccnature</nature> http://www.openstm32.org/forumthread1244。这可以在 Eclipse 运行且项目打开时完成。结果:main.cpp 编译但找不到包含文件,因为包含路径错误。你会认为cppnature会是一个更好的名字。编辑:在 Eclipse 帮助中搜索“C++ 自然”,获取有关如何使用菜单文件/新建/其他/转换为 C/C++ 制作项目添加 C++ 自然的提示。
  • 接下来,修改项目设置以将相关的 C 设置复制到 C++ 设置 - 如此处所示。在此处输入图片说明需要关闭并重新打开项目才能使此更改生效。结果:构建、链接、运行和正确运行。main.cpp 可以调用 HAL c 代码,HAL 回调可以调用 .cpp 文件中的函数。.cpp 文件中可能需要 extern "C" 修饰符,如果1.cpp 端正在调用 .c 端的函数(通过预先挂起修改函数原型defined extern "C"2.c 端正在调用 .cpp 中定义的回调) (通过前置 .cpp 修改 .cpp 端的函数定义defined extern "C"

Some notes:

一些注意事项:

  • If I ever re-generate code with CubeMX (e.g., to change a clock or pin-configuration), main.c will get updated but main.cpp will not - so it's necessary to merge changes/diffs from the .c file over to the .cpp file.
  • How to be certain the C & C++ settings are equivalent: After a build, look at the Console window and compare the command-line invocation for gcc and g++ to make sure pertinent parameters (paths, -D etc) are identical.
  • It's possible (but risky) to edit the Eclipse .cproject XML file and replicate the C settings over to the C++ side (specific details omitted here - but close project in Eclipse before editing the .cproject file).
  • Regarding objections to CubeMX/HAL 'bloat': CubeMX generates code and projects very quickly. If a HAL API is slow - e.g., GPIO port-pin access - then simply replace the API call with a new user-function containing a simpler subset of what that bloated API does. I do this frequently in time-critical code.
  • I'm using CubeMX (4.14.0) and Atollic (v6.0.0 Lite). I didn't see the project option to convert to C++ in the Atollic Eclipse although I have seen that option in other Eclipse environments e.g., Xilinx Eclipse.
  • 如果我使用 CubeMX 重新生成代码(例如,更改时钟或引脚配置),main.c 将更新,但 main.cpp 不会 - 因此有必要将 .c 文件中的更改/差异合并到.cpp 文件。
  • 如何确定 C 和 C++ 设置等效:构建后,查看控制台窗口并比较 gcc 和 g++ 的命令行调用,以确保相关参数(路径、-D 等)相同。
  • 可以(但有风险)编辑 Eclipse .cproject XML 文件并将 C 设置复制到 C++ 端(此处省略了特定细节 - 但在编辑 .cproject 文件之前关闭 Eclipse 中的项目)。
  • 关于对 CubeMX/HAL“膨胀”的反对意见:CubeMX 生成代码和项目的速度非常快。如果 HAL API 很慢——例如,GPIO 端口引脚访问——那么只需用一个新的用户函数替换 API 调用,该函数包含那个臃肿的 API 所做的更简单的子集。我经常在时间关键的代码中这样做。
  • 我正在使用 CubeMX (4.14.0) 和 Atollic (v6.0.0 Lite)。我没有在 Atollic Eclipse 中看到转换为 C++ 的项目选项,尽管我在其他 Eclipse 环境(例如 Xilinx Eclipse)中看到了该选项。

Room for improvement: There might be a CubeMX template that could be edited to make these changes more automatic but I don't know enough about CubeMX templates to make this hack more elegant.

改进空间:可能有一个 CubeMX 模板可以编辑,使这些更改更加自动化,但我对 CubeMX 模板了解得不够多,无法使这个 hack 更加优雅。

I use CubeMX with IAR EWARM too and go through a similar (but slightly easier) process to convert to a .cpp project.

我也将 CubeMX 与 IAR EWARM 一起使用,并通过类似(但稍微简单一些)的过程转换为 .cpp 项目。

回答by Timmmm

Depending on which STM32 chip it is, it might be supported by mBed. The online mBed compiler allows exporting to a variety of IDEs. The new offline one (yotta) uses cmake which can export to some IDEs too. Unfortunately yotta only supports a couple of chips at the moment.

根据它是哪种 STM32 芯片,mBed 可能支持它。在线 mBed 编译器允许导出到各种 IDE。新的离线 (yotta) 使用 cmake,它也可以导出到一些 IDE。不幸的是,yotta 目前只支持几个芯片。