I am using TrueStudio for my own stm32 project. I create 2 file foo.h and foo.c includes 2 functions
//foo.h
int add(int a, int b);
int sub(int a, int b);
and the implementation of timeout
//foo.c
#include "foo.h"
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
After that, I used gcc to compile a static library foo.a. I continue to make the main file to implement the library to test.
//main.c
#include <stdio.h>
#include "foo.h"
int main(int argc, const char *argv[])
{
int a = 100, b = 50;
printf("sum is: %d\n", add(a,b));
printf("sub is: %d\n", sub(a,b));
return 0;
}
Next, I link the static foo lib to main.c to make an executable file using command is
gcc main.c foo.a -o main
I ran it and get the result is
sum is: 150
sub is: 50
That's worked fine prove my static lib was built successfully. I begin to create a project stm32 from stmcubeMX and linker to this foo.a and the error appeared.
undefined reference to 'add'
undefined reference to 'sub'
My full code and setting path and build bellow
//main.c in TrueStudio
#include "main.h"
#include "foo.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
int a = 200, b = 100;
int _sum, _sub;
_sum = add(a, b);
_sub = sub(a, b);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
I am very grateful for any help, thanks!
Rename your
foo.a
file tolibfoo.a
, then change the C Linker -> Libraries -> Libraries to justfoo
with nothing in front or in the back. This should cause the final output to be-lfoo
, which in turn causes linker to search forlibfoo.a
in the library search paths.