Where to add new .h and .cpp file in STM32CubeIDE project - stm32cubeide

I used STM32CubeIDE to create a new (C++) project for my nucleo-f411re board by File > New > STM32 Project. The project was created as usual. I renamed "main.c" to "main.cpp" since I selected C++ as the target language. A simple "blinky" compiles and runs correctly on the board. So now I want to begin adding some C++ code to the project.
The "main.cpp" is in the project tree under myproject > Core > Src > main.cpp. If I want to create some new source/header files, say "foo.h" and "foo.cpp", I think I can right-click on something in the project tree, then New > Header File or New > Source File. But the question is -- what do I right-click on to create the source/header file? I assume the file will be created in the location (= folder) I right-click on. Where do I want to put them? Both of them in the "Core" folder? Separately in "Core > Inc" and "Core > Src" folders? I'm not sure.
Edit: I added the header/source files to Core > Inc and Core > Src respectively, and that seems to work. Is that recommended? I wonder if they will still be there if I change the configuration, causing the code to be regenerated.

I know this is a very late answer but I didn't find a lot of tips regarding this question a while ago so here is my take on it.
Basically you can place source file in any source folder.
You can use existing folders or create a new one, however you like.
Header files have to be placed inside a folder that is in the include path (e.g. Core/Inc) or you have to tell the compiler that a specific file should be included.
If you want to create a new folder for your header files you have to add the folder or files inside this folder to the include path via Project -> Properties -> C/C++ Build -> Tool Setting (tab) -> MCU GCC Compiler or MCU G++ Compiler -> Include paths.
Note that you might have to add it twice, once for C files (MCU GCC Compiler) and once for C++ files (MCU G++ Compiler).
To create a file you can simply click on the folder you want to create it in and add a new <whatever>file.
You can also just create files and directories on your local filesystem and refresh the project (right click -> Refresh or F5) to show changes in STM32CubeIDE.
For small projects (like a blinky project) it is probably enough to just use the Core folder.
For bigger projects I personally like to seperate all the autogenerated code from my own files.
Especially for C++ projects it can become very annoying that STM32CubeIDE always renames main.cpp to main.c and deletes code segments you accidentally put outside their comment blocks every time you run the code generation.
I usually create a Project source folder for project specific files and a Lib source folder for libraries.
Then I create a cppmain.h file that simply declares a void cppMain() function and a cppmain.cpp file that I use to write my main program.
The only change to the autogenerated code is that you have to include cppmain.h and call cppMain() before the main loop in main.c.
Directory tree:
Project name
+ Core
| + ...
+ Drivers
| + ...
+ Libs
| + someLib
| | + Inc
| | | + someLib.h or hpp
| | + Src
| | + someLib.c or cpp
| + ...
+ Program
| + Inc
| | + cppmain.h
| | + ...
| + Src
| + cppmain.cpp
| + ...
+ ...
You will have to add Program/Inc and any Lib/Inc folder to the include path.
The changes to main.c then look like this:
...
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "cppmain.h" // this goes in user includes
/* USER CODE END Includes */
...
/* USER CODE BEGIN 2 */
cppMain(); // this goes before the main loop
// anything after this point will never be reaced
// when you create a main loop inside cppMain()
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
...
Inside the cppmain.h file I usually #include "main.h" to get all the STM32 HAL stuff and declare all hardware resource handles as extern (e.g. extern UART_HandleTypeDef huart1;)
The full cppmain.h file:
/** #file cppmain.h
*
* #author some_author
* #date some_date
*/
#ifndef PROGRAM_INC_CPPMAIN_H_
#define PROGRAM_INC_CPPMAIN_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "main.h"
void cppMain(void);
#ifdef __cplusplus
}
#endif
#endif /* PROGRAM_INC_CPPMAIN_H_ */
Some random cppmain.cpp file:
/** #file cppmain.cpp
*
* #author some_author
* #date some_date
*/
#include "cppmain.h"
#include "pins.h"
// pins is useful when you connect some things to a dev board
// and want to have a quick reference where you put stuff
// this just includes things like (for STM32F103xB "blue pill" in this case)
// #define LED_PIN_PORT GPIOC
// #define LED_PIN_PIN GPIO_PIN_13
// #define LED_PIN LED_PIN_PORT, LED_PIN_PIN
extern UART_HandleTypeDef huart1;
int some_variable = 0;
void foo(int input){
// do something
}
void cppMain(){
// do stuff
// main loop
while(1){
// do stuff regularly
}
}
In my Lib folder I usually link to libraries I want to change for all projects while working on them: right click on Libs -> New -> Folder -> Advanced >> -> Link to alternate location.
Note that this will link to an absolute path on your system and you can't simply import your project on a different system this way.
If you work in a team or across different devices you might want to solve this a different way.

I am using STM32CubeIDE version 1.8, it can let you add include folder directly from the project menu -> Add/remove include path:
To add source folder, you can create source folder from the project menu -> New -> Source Folder:
If you want to add include and source folders manually:
Project Properties-> C/C++ General->Paths and Symbols:
Select Includes tab to add header folder
Select Source Location to add source folder

Related

It errors when specifying the user defined java library into RED robot framework eclipse editor

My requirement is to make use of user defined java libraries in robot framework using RED eclipse editor. When trying to specify library in the robot framework, the system errors as no such library available(shown underline in red for library name). Please correct my mistakes done. I have followed the below steps,
Updated Eclipse with RED Editor(Eclipse Neon (v 4.6), RED - Robot Editor v0.7.5)
Created a class file just as Project in the same eclipse. (Package name: org.robot.KCCKeywords and Class Name: LogonToKCC)
Converted the class file into the type '.JAR' and stored it in jython folder(C:\jython2.7.0\Lib\site-packages\KCCLibraries)
Integrated RED with Maven plugin using launch4j-3.8-win32(using https://github.com/nokia/RED/blob/9d62dccce18ee7f3051162d05bf3d027e33dccef/red_help/user_guide/maven.html.md)
Integrated RED with Robot framework and Jython. (using https://github.com/nokia/RED/blob/9d62dccce18ee7f3051162d05bf3d027e33dccef/red_help/user_guide/maven.html.md)
CLASS PATH updated for below jars,
a) jython.jar
b) robotframework-3.0.2.jar
c) myOwnJavaLibrary.jar ( The jar that i created in step 3)
d) jdk and jre path
Verified the same class paths in red.xml too.
Created RED Project and started initializing key words as below,
a) Library Selenium2Library
b) Library org.robot.KCCKeywords.LogonToKCC
Here is where the system couldn't read my own library.
I also referred to below blogs and adjusted my steps accordingly. But didn't help me. Referring to multiple blogs and stacks also confusing me. Finally I'm here.
robot framework user java libraries error Test Library "mavenPackage.MyKeyWords.java" does not exist
Robot Framework-RIDE,Import Java Libraries
Stuck with creating Keyword library using Java in Eclipse and using that JAR file in RIDE
Robot Framework - using User Libraries
Using the codecentric blog: Robot Framework Tutorial – Writing Keyword Libraries in Java as a base with some specific steps for RED in stead of RIDE. This walkthrough will allow you to setup Jython, create a simple library in Java and run it from Robot script.
After the installation of Eclipse (NEON) and RED Feature in Eclipse create a new Java project in Eclipse. With that done continue to create a new Java class with the following content.
package org.robot.sample.keywords;
import java.util.Stack;
/**
* This is an example for a Keyword Library for the Robot Framework.
* #author thomas.jaspers
*/
public class SampleKeywordLibrary {
/** This means the same instance of this class is used throughout
* the lifecycle of a Robot Framework test execution.
*/
public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";
//</editor-fold>
/** The Functionality to be tested */
private Stack<String> testStack;
/**
* Keyword-method to create an empty stack.
*/
public void createAnEmptyStack() {
testStack = new Stack<String>();
}
/**
* Keyword-method to add an element to the stack.
* #param element The element
*/
public void addAnElement(String element) {
testStack.push(element);
}
/**
* Keyword-method to remove the last element from the stack.
*/
public void removeLastElement() {
testStack.pop();
}
/**
* Keyword-method to search for an element position.
* #param element The element
* #param pos The expected position
*/
public void elementShouldBeAtPosition(String element, int pos)
throws Exception {
if (testStack.search(element) != pos) {
throw new Exception("Wrong position: " + testStack.search(element));
}
}
/**
* Keyword-method to check the last element in the stack.
* #param result Expected resulting element
*/
public void theLastElementShouldBe(String result) throws Exception {
String element = testStack.pop();
if (!result.equals(element)) {
throw new Exception("Wrong element: " + element);
}
}
}
Please ensure that you have Jython installed using the Windows Installer. In my example Jython is installed in c:\Jython. As with the regular Python Interpreter Robot Framework still needs to be installed. Assuming that your machine has access to the internet, in the command line go to c:\Jython\bin\ and run the command pip install robotframework. This will install Robot Framework in the Jython environment.
Now create a new Robot Framework project in Eclipse. Please make sure that you have Window > Perspective > Open Perspective > Robot or Other > Robot.
In the project the default Robot Framework is one based on Python and we need to configure the Jython Interpreter. In Eclipse go to Window > Preferences and then select Robot Framework > Installed Frameworks from the tree-menu. Click on Add and point to c:\Jython\bin\. Click on OK.
Open Red.XML from the Robot Project and go to the general tab. This is where the project Interpreter is set. If it is set to Python (like the example below) then click on use local settings for this project and check the Jython interpreter. Save the settings to the file (CTRL-S).
With Robot Framework project setup it is time to export the Java class to a Jar file. Right click the class file and click export. Then choose JAR file and click next. Click on Browse and set the location and file name of the JAR file. In this case I picked ExampleLibrary.jar and the folder of my Robot Project. Press Finish to complete the export.
Go Back to Red.XML and click on Referenced Libraries then proceed to click on Add Java library, pick the exported Jar file (ExampleLibrary.jar) and press OK. This will proceed to load the jar and read the keywords from the Jar file. Save the file (CTRL-S). This should result to the below reference.
Now it's time to create a Robot file and use the library. In the referenced blog the following example script is given that uses the java functions/keywords.
*** Settings ***
Library org.robot.sample.keywords.SampleKeywordLibrary
*** Test Cases ***
ExampleJava
Create An Empty Stack
Add An Element Java
Add An Element C++
Remove Last Element
The Last Element Should Be Java
With the already loaded library no red lines should appear, but otherwise right-click on the library and click quick-fixand autodiscover the library.
Then using the regular Eclipse/RED Run menu run the script. This will then run the script successfully and output the following:
Command: C:\jython2.7.0\bin\jython.exe -J-Dpython.path=C:\jython2.7.0\Lib\site-packages -J-cp .;C:\Eclipse\Workspace\ExamplJava\ExampleLibrary.jar -m robot.run --listener C:\Users\User\AppData\Local\Temp\RobotTempDir8926065561484828569\TestRunnerAgent.py:57292:False -s ExamplJava.ExampleJava C:\Eclipse\Workspace\ExamplJava
Suite Executor: Robot Framework 3.0.2 (Jython 2.7.0 on java1.8.0_60)
==============================================================================
ExamplJava
==============================================================================
ExamplJava.ExampleJava
==============================================================================
ExampleJava | PASS |
------------------------------------------------------------------------------
ExamplJava.ExampleJava | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
ExamplJava | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: C:\Eclipse\Workspace\ExamplJava\output.xml
Log: C:\Eclipse\Workspace\ExamplJava\log.html
Report: C:\Eclipse\Workspace\ExamplJava\report.html
I finally followed the below for a great journey with robot framework.
1 Installed Java, Eclipse, RED Eclipse plugin.
a) Java(JDK 1.8.0/JRE 1.8.0)
b) Eclipse Neon (v 4.6)
c) RED - Robot Eclipse Editor v0.7.5.2(Eclipse Plugin)
2 Downloaded and Installed Python 2.7.12 using windows. A folder created automatically after installation in C:\python27
3 "Installed Robot Framework using pip command in Command Prompt.
Command: C:\python27\scripts>pip install robotframework"
4 Downloaded and installed Jython 2.7.0 using windows. A folder created automatically after installation in C:\jython2.7.0
5 "Installed Robot Framework using pip command in Command Prompt.
Command: C:\jython2.7.0\bin>pip install robotframework"
6 "Installed Selenium2Library using pip command in Command Prompt.
Command: C:\jython2.7.0\bin>pip install robotframework-selenium2library"
7 "Set the below,
a) Goto Window-Preferences-Robot Framework-Installed Framework
b) Map Robot framework with Jython Interpreter
I used c:\jython2.7.0\bin"
8 Created JavaProject and export it into a jar file. Right click on the class name, click on export-Java-Jarfile. Select the path name where the new jar file to be put including the new file name.jar. Click Ok.
9 Open RED.xml Click Add Java Library and select the newly created jar file.
10 "Set up this before proceeding with robot framework
goto Windows - Perspective - Open Perspective-Other-Robot"
11 Create a robot suite, import library selenium2library & user defined library, Write Test cases.

NetBeans not parsing CMake defines

I'm using NetBeans to work on an existing CMake project.
In CMakeLists.txt there is
set(DEFINES ${SRC}/defines.cmake)
So when in source you have
#ifdef MY_OPT
//do stuff
#endif
these are blanked out in NetBeans, because there is never a
#define MY_OPT
Instead there is a
-DMY_OPT
in defines.cmake.
How can I tell NetBeans to look into this file (and others)?
set(DEFINES ${SRC}/defines.cmake)
What is this line intended to do? If your defines.cmake contains some preprocessor settings, you better use include():
include("${SRC}/defines.cmake")
Is it possible that this defines.cmake file actually sets the defines through the pre-processor instead?
Yes, this is where add_definitions() will help:
add_definitions(-DMY_OPT -DANOTHER_ONE)
To sum it up:
CMakeLists.txt
# ...
include("${SRC}/defines.cmake")
# ...
defines.cmake
# Add defines
add_definitions(-DMY_OPT)
Somewhere in your source:
#ifdef MY_OPT
/* This is used now */
#else
/* MY_OPT is not defined - shouldn't happen now */
#endif
Btw., you may also have a look at configure_file().

Clion 1.1.1 use CMakeList.txt to add library headers for unity

I never used CMake before so please excuse me.
My project has a "unity" folder that contains version 2.3.0 of the unit test library). unity_fixture.h contains "#define TEST(..." which is used like the following:
#include "unity_fixture.h"
...
TEST(xformatc, Simple) {
char output[20];
TEST_ASSERT_EQUAL(13, testFormat(output, "Hello world\r\n"));
TEST_ASSERT_EQUAL_STRING("Hello world\r\n", output);
}
I added "include_directories(${CMAKE_SOURCE_DIR}/unity)" to my CMakeLists.txt file. Still CLion does not find the declaration of TEST and I get tons of errors.
I did try to add all the unity files with set(SOURCE_FILES unity/unity_fixture.h..." but this did not work either.
edit 08.09.2015
I found out something strange. If I call cmake from command line it creates a file "DependInfo.cmake" with the following contents:
# The set of languages for which implicit dependencies are needed:
set(CMAKE_DEPENDS_LANGUAGES
)
# The set of files for implicit dependencies of each language:
# Targets to which this target links.
set(CMAKE_TARGET_LINKED_INFO_FILES
)
# The include file search paths:
set(CMAKE_C_TARGET_INCLUDE_PATH
"unity"
"cmsis/inc"
"freertos/inc"
)
set(CMAKE_CXX_TARGET_INCLUDE_PATH ${CMAKE_C_TARGET_INCLUDE_PATH})
set(CMAKE_Fortran_TARGET_INCLUDE_PATH ${CMAKE_C_TARGET_INCLUDE_PATH})
set(CMAKE_ASM_TARGET_INCLUDE_PATH ${CMAKE_C_TARGET_INCLUDE_PATH})
The CMAKE_C_TARGET_INCLUDE_PATH stuff is missing in the file that is created by CLion. I believe that is the reason why it does not find the headers. Question is how do I tell CLion to create the CMAKE_C_TARGET_INCLUDE_PATH stuff?
I assume that your project structure is :
project root
├── CMakeLists.txt
├── Some source files
└── unity
└── unity_fixture.h
If you use CMake to include files :
set(INCLUDE_DIR ./unity)
include_directories(${INCLUDE_DIR})
Your include must be : #include <unity_fixture.h>
Or you can use without using CMake to include directories : #include "unity/unity_fixture.h"

Adding QtCore Library in blackberry 10 sdk

Hi guys I am creating a simple game using cocos2d-x and blackberry. I need some place to store my game settings, something similar to shared preferences in ios and android. I found some code using qsettings, but the problem is I am not able to add the QtCore library.
I add the library using RightClick->configure->add Library and Standard BlackBerry Platform Library. The library gets added successfully.
#include "dataProcessor.h"
#include <QtCore>
void dataProcessor::setup(){
QDir dir;
dir.mkpath("data/files/text");
dir.cd("data/files/text");
}
but when I compile the above code, I get the error C:/Users/I076636/Documents/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h:45:28: fatal error: QtCore/qglobal.h: No such file or directory
But I noticed 2 things,
1.qglobal.h file is there inside the QtCore directory I have included.
2.inside qatomic.h if I change
#ifndef QATOMIC_H
#define QATOMIC_H
#include <QtCore/qglobal.h>
#include <QtCore/qbasicatomic.h>
into
#ifndef QATOMIC_H
#define QATOMIC_H
#include <qglobal.h>
#include <QtCore/qbasicatomic.h>
the error for qglobal goes and now the same error comes for qbasicatomic.h.
I think it is something simple like incorrect mapping between QtCore keyword and include directory or something..
Please do have a look.
The IDE is made on eclipse.
You can understand what is going wrong if you look closely at the error message:
/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h:45:28:
fatal error: QtCore/qglobal.h: No such file or directory
The error isn't in your inclusion of QtCore, but is occurring inside QtCore/qatomic.h, on line 45 (you can find this file in the [YOUR BBNDK DIRECTORY]/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h):
#include <QtCore/qglobal.h>
qatomic.h is already in the QtCore directory, and you'll find a qglobal.h directory there as well. So what this means is that qatomic.h expects the parent directory to be on the include path, so that including <QtCore/qglobal.h> will work.
So you just need to add [YOUR BBNDK DIRECTORY]/target_10_0_9_1673/qnx6/usr/include/qt4 to your include directories.
Do it like this:
Right click over your project in Project Explorer and choose Properties
Expand the tree to C/C++ General / Paths and Symbols
Change the Configuration in the Paths and Symbols frame to [All configurations]
Click the Includes tag and select GNU C in the Languages list (or do this for every language).
Click Add... and type ${QNX_TARGET}/usr/include/qt4 and press OK
Click Add... and type ${QNX_TARGET}/usr/include/qt4/QtCore and press OK
Now your include of #include <QtCore> should work.
Next up: linking errors ;-)
It sounds like your BB10 NDK did not get installed properly, or your project wasn't set up properly. If you expand your project and the Includes you should see (along with others):
<NDK_INSTALL_LOCATION>/target_<VERSION>/qnx6/usr/include/qt4/QtCore

Using CMake to index source files of an external library with Eclipse

I am using CMake to build a project with external libraries by using "Eclipse CDT4 - Unix Makefiles".
Importing in Eclipse leads to a working project, but only all header files and my implemented source files are recognized correctly by the index of Eclipse.
I would also like to navigate through the source files for one external library by using "ctrl+click". I don't know how to add the *.cpp files of that external library in my CMakeList.txt to get them recognized by the indexer without building the library.
You can mark the .cpp files as "header file only" like this:
# find all filenames in the lib path and gather them in $YOUR_LIB
FILE(GLOB YOUR_LIB path_to_library/*.?pp)
# create a seperate sourcegroup so it doesn't clutter up the rest of your code
SOURCE_GROUP(\\lib FILES ${YOUR_LIB})
# mark them as header-file only
SET_SOURCE_FILES_PROPERTIES(${YOUR_LIB} PROPERTIES HEADER_FILE_ONLY TRUE)
# add both your code and the lib-code to the project
ADD_EXECUTABLE(program ${YOUR_CODE} ${YOUR_LIB})
I found a way to attach external library source files to the Eclipse project that is compatible with CMake project generator.
It turns out that to indexing and "ctrl+click" navigation works correctly only when external library sources are direct descendants of the project source folder. Therefore the solution is following:
Scan external library folder for source files.
Create a child folder under project's source folder.
Symlink discovered sources inside the created folder.
I created a CMake function attachExternalSources that performs above steps:
function(attachExternalSources librarySourceLocation folderName)
# Create folder for Geant4 sources
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/${folderName})
message(STATUS "Searching for C++ sources in \"${librarySourceLocation}\"...")
FILE(GLOB_RECURSE libSources
${librarySourceLocation}/*.c
${librarySourceLocation}/*.cpp
${librarySourceLocation}/*.cxx
${librarySourceLocation}/*.cc
)
message(STATUS "Symlinking sources into\n \"${CMAKE_SOURCE_DIR}/${folderName}\"\n Please wait...")
foreach(source ${libSources})
# Obtain source filename
get_filename_component(source_filename ${source} NAME)
# Create symlink unless it already exists
set(symlink "${CMAKE_SOURCE_DIR}/${folderName}/${source_filename}")
if(NOT EXISTS ${symlink})
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${source} ${symlink})
endif()
endforeach()
# Scan all the symlinks created under the project folder and disable their compilation
FILE(GLOB sources_symlinks ${CMAKE_SOURCE_DIR}/${folderName}/*)
SET_SOURCE_FILES_PROPERTIES(${sources_symlinks} PROPERTIES HEADER_FILE_ONLY TRUE)
endfunction()
The use of the function is following. Paste above function code in your CMakeLists.txt. Next, use it as follows:
attachExternalSources("path/to/external/library/sources" "library-sources")
First parameter is location of the external library source code. Second argument is the name of a folder inside your project that that will contain source symlinks.
P.S. I tested function with Eclipse 4.19 and CMake 3.20.5.