Skip to main content
HPham.1590
Associate III
August 12, 2020
Solved

How to link library (.c and .h file) in CubeIDE?

  • August 12, 2020
  • 7 replies
  • 11800 views

Hello, I'm new in STM32CubeIDE.

I want to create my library to use in my C/C++ project.

My library includes 2 files (.c and .h) but I don't know how to linking them in CubeIDE.

Anyone can help me?

Many thanks,

Hieu

This topic has been closed for replies.
Best answer by TDK

Drag them into the Core/Src and Core/Inc (or Src and Inc) folders within your project. Add an #include statement for the header and use them in whatever file you need.

7 replies

TDK
TDKBest answer
Super User
August 12, 2020

Drag them into the Core/Src and Core/Inc (or Src and Inc) folders within your project. Add an #include statement for the header and use them in whatever file you need.

"If you feel a post has answered your question, please click ""Accept as Solution""."
HPham.1590
Associate III
August 13, 2020

Thanks for your reply, but when i using CubeIDE, I added these file to locations as your refer but when I call function in this lib from other file, it still jump to .h file, not .c file.

Here is my simple .h file

#ifndef __GETCONFIG_H__
#define __GETCONFIG_H__
void SimJSON();
void GetConfig();
#endif

and .c file

#include "GetConfig.h"
#include "cJSON.h"
void SimJSON()
{
 
}
void GetCongif()
{
	SimJSON();
}

TDK
Super User
August 13, 2020

Not sure what you mean. Your .h file doesn't have any code, just definitions. The processor can't jump there.

"If you feel a post has answered your question, please click ""Accept as Solution""."
prain
Visitor II
August 12, 2020

In cubeide you don't need to register source files for compiler. There are directories under project root, named source folders, that you can put compilable files into. just copy paste your files in a source directory and you are good to go. you can later exclude files from compile through project options.​

HPham.1590
Associate III
August 13, 2020

but I dont know how to link these file. My function define in .h but written in .c file

KnarfB
Super User
August 13, 2020

A .h plus .c file is just source code, not a library that needs extra consideration for linking. If you follow the above instructions, you should see the .c file being compiled in the CDT Console Window.

Are you getting errors? What exactly?

dbgarasiya
Senior
August 14, 2020

just follow given instructions

HPham.1590
Associate III
August 14, 2020

Well, that's my mistake. It's still linking but I tried to call this C lib in C++ file.

Sorry,

Hieu

dbgarasiya
Senior
August 18, 2020

glad to hear that

JB.2
Associate
May 6, 2021

Hello!

I try to link an exisiting library (.h and .cpp files) to a new project but the compilator dont see it

I'v drag the library folder into my project and selected "link files and create virtual folders"

And added de dependancies in the following entries:

In "Linked ressources" in "Path Variable"

In "Path and symbols" in "Include", "Library Path" and "Source Location"

in "Preprocessor Include Path Macros ..." in "CDT User Settings"

I'v put the path to the library folder where I thought it was necesary and after i'v put it everywhere I could >.<

There is no weird characters or spaces in the full path of the project or library.

i'v also tryed to put the linkes to the files in core/src and core/inc or in Middlewares but same issue.

I realy need the files to be linked and not in copie because they are used in the windows client and for the two core projects.

Did I missed or do something wrong ?

Thank you for your help !

Version: 1.6.1

Build: 9958_20210326_1446 (UTC)

Fresh and working project

Win10 Nucleo 144h745zi

TDK
Super User
May 6, 2021

Include paths are be in project properties -> C/C++ Build -> Settings -> Tool Settings -> MCU G++ Compiler -> Include paths. If that doesn't fix it, make a new thread and show a screenshot of your include paths.

"If you feel a post has answered your question, please click ""Accept as Solution""."
JB.2
Associate
May 6, 2021

Hello, thank you for your answer !

But the path was already there...

I'v created a new topic on the forum

Deepak.Aagri
Associate III
February 5, 2022

Yes this works ...

Include paths are be in project properties -> C/C++ Build -> Settings -> Tool Settings -> MCU G++ Compiler -> Include paths.

>Click >> Workspace

0693W00000JOhPNQA1.png