I'm trying to create library this is my code
Why I get on :
undefined reference to addTwoInts(signed char, int)'`
Test.h file
#include "stdint.h"
#ifndef TEST_H
#define TEST_H
void addTwoInts(int8_t a, int b);
#endif
Test.c file
#include "Test.h"
#include "stdint.h"
void addTwoInts(int8_t a, int b)
{
printf("The result is =%d", a + b);
}
main arduino
#include "Test.h"
void setup()
{
Serial.begin(9600);
addTwoInts(4,3);
}
void loop() {}
Where did you put Test.h and Test.c?
in0
April 8, 2022, 8:15pm
4
Hi @alex_al . The reason for the error is that, after some minor preprocessing , the .ino files of the sketch are compiled as C++, but Test.c is compiled as C because you used the .c file extension.
The easiest fix is to simply change the file name to Test.cpp. That will cause it to be compiled as C++. You will find that convenient because most of the Arduino core API and libraries are also C++, so writing your code in C++ will allow you to use them.
If you really do want to use C, you can wrap the declarations in extern "C" {}:
extern "C" {
#include "Test.h"
}
Hi @in0 , use this
"C" {}
in Test.c ? because I get on expected identifier or '(' before string constant
in0
April 8, 2022, 8:40pm
6
No. Replace this line in your .ino file:
#include "Test.h"
with this:
extern "C" {
#include "Test.h"
}
You could also wrap the declarations in the header file instead, using this common construct:
#ifdef __cplusplus
extern "C" {
#endif
// all of your legacy C code here
#ifdef __cplusplus
}
#endif
The header file be like this ?
#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h"
#ifndef TEST_H
#define TEST_H
void addTwoInts(int8_t a, int b);
#endif
#ifdef __cplusplus
}
#endif
in0
April 8, 2022, 9:17pm
8
Better like this:
#ifndef TEST_H
#define TEST_H
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
#endif
void addTwoInts(int8_t a, int b);
#ifdef __cplusplus
}
#endif
#endif
in0
April 8, 2022, 9:20pm
10
You are welcome. Best wishes for success with your library!
Hello, other relevant question. I have lib contains on many functions look like this
mainlib.h // in side this file mainlib.h I have
lib1.h
bib2.h
lib3.h
// functions
every think work well but When import this mainlib.h inside
the sketch I get on all this headers
#include <mainlib.h>
#include <lib1.h>
#include <bib2.h>
#include <lib3.h>
I need to import and show just the main library "mainlib.h "
Why did you create a new topic, particularly when you originally added your new question here only to delete it ?
Because I got the answer to my previous question so I wanted to add this question as a new topic.
system
Closed
October 7, 2022, 7:13pm
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.