Context:
I would like to add a custom library to a piece of Arduino code in the Arduino 1.5.7 IDE to ensure code is decentralized and readable
Attempted solution:
I make a folder called "mathsfunctions". In it I put two text files, one with a .c and another with a .h name extension.
The .c file is called "mathsfunctions.c" and has the following code in it:
#include "mathsfunctions.h"
int multiply (int a, int b)
{
return a*b;
}
The .h file is called "mathsfunctions.h" and has the following code in it:
int multiply (int, int);
In the main file, I add in the following include preprocessor directive:
#include "mathsfunctions.h"
//The rest of the code
After the above was coded, I imported the library. To do this, I did the following:
Toolbar -> Sketch -> Add Library -> c:.....\mathsfunctions
I can confirm that this is indeed imported because after doing such action, the same mathsfunctions folder appears in the Arduino libraries folder:
C:.....\Arduino\libraries\mathsfunctions
Problem: Upon compiling, the error dialogue box gives the following error:
mathsfunctions.h: No such file or directory
Assistance Required: Any idea on what the problem could be?
You should only have put the header and the source in the same directory as your main file. Also I would suggest putting the implementation in the header since this is the way that people generally include extra functions in C. I am unsure if C supports extra source files but it does support extra headers.
Related
I am just starting out with Segger Embedded Studio. Right now trying to call a function of a included header file. The file seems to be included since it shows up in the dependencies. For now I simply included the header and C-File in the project directory.
The included Header- and C-Files are simply:
//##### Header-File ######
#ifndef TEST_H_
#define TEST_H_
void printText(void);
#endif //TEST_H_
and:
//###### C-File #########
#include <test.h>
#include <stdio.h>
#include <stdlib.h>
void printText(void)
{
printf("Hello");
}
But when I try to call the printText Function in my main I get the error:
"Undefined Symbol: printText".
Why is the function not recognized?
"Undefined symbol" is a linker error. You are not linking the object code containing the definition of printText().
It is not an issue with the header file; including a header file does not cause the associated code to be linked - that is just the declaration so the compiler knows what the interface looks like. It is the linker than combines the separately compiled object code to form a program. You have not told the linker to use the object code containing printText(), and you have not told your IDE project to compile it to generate that object code.
The project tree clearly shows that only main.c is included in your project; you need to add the C file containing printText() too.
The concept of separate compilation and linking is what you need to grasp here.
Thank you Clifford for your answer. You are right I had some miss-conceptions about which files will be linked while building the project. In the special case of segger embedded studio there are, as I know by now, two ways to reference extern files.
Adding the files to the sources files folder is straight forward, but must be done separately for each project you want to use the respective files.
For frequently used files it is beneficial to create a dedicated library solution. Such a library can then be imported to any solution by choosing "add existing project". This will add all files of the library to your current solution (and show them in the project-tree). Now click the tab project -> dependencies. Your library should show up here. By acitivating the check box the linker will compile the referenced project upon builing your solution, allowing for the usage of your library functions.
Adding and linking library-project to a current solution
I'm trying to include a folder that contains a combination of around 60 .h and .hpp files. This folder contains libraries for programming robots with a Wallaby (a mini-computer-like device) for Botball competition. include is located in the same place as main.c (inside code). Up until now, this is what my header for including libraries looks like:
#include "../code/include/accel.h"
Just like accel.h, I have 60 other .h and .hpp files inside include. So, coming to my question, do I need to type out all the 60 header lines? or is there a way to include the include folder.
I'm using Clion for this project, if I can't include the folder itself, does anyone know of a shortcut in Clion to include all the files in include.
I was also thinking of using some sort of placeholder for the folder name and only specify the file type. So, for example: #include "../code/include/(generic placeholder name).h". I have no clue if something like this exists.
I would also request you to keep in mind that I'm a beginner to programming, so please keep your answers simple.
This is just for some extra info:
The Wallaby is a mini computer to which you would connect your sensors, motors, servos and cameras in order to control a robot for the Botball competition. Usually, one can connect to the Wallaby either via Wifi Direct or a cable and write programs on it directly through an online interface (not entirely sure of the word for it, but you just type in an IP address in your browser and it brings up an interface where you can make projects and code). All the code written in that interface saves directly onto the Wallaby. Here the default include statement is #include <kipr/botball.h>, so I'm assuming that botball.h (which is located on the Wallaby's storage) has all those 60 libraries consolidated in it. I got the include folder that I'm using from GitHub. This link was provided to me by one of the Botball organisers. So the main point in me trying to download the library is so that I can write and successfully compile code even when I'm not connected to the Wallaby. Hope this provides some relevant context.
Thank you for your answers!
What I'd do is
Create (maybe with scripting tools or a specific program) a "all.h" file which includes all the other header files
#ifndef ALL_INCLUDED
#define ALL_INCLUDED
#include "accel.h"
#include "bccel.h"
//...
#include "zccel.h"
#endif
Include "all.h" in your main file
#include "../code/include/all.h"
You can create "all.h" automatically every time you build your code.
CLion is an IDE for Clang and GCC. These compilers are instructed to search paths for include files by specifying -I<path> command line arguments. Any number may be specified, and they are searched in the order given, and the first match found is the file that gets included.
I am not familiar with CLion specifically but no doubt it has a dialog somewhere where you can set header file search paths.
Edit: It seems that CLion may not make this so straightforward. I understand that you have to add then via CMake: https://cmake.org/cmake/help/v3.0/command/include_directories.html#command:include_directories, but after that, the IDE will not recognise the header in the editor and will warn you of unrecognised files and will not provide code comprehension features. I believe it will build nonetheless.
I'm facing a problem that I don't know how to solve.
Suppose the following typedef struct into a test.h header file:
typedef struct example_struct {
#ifdef CONFIG_A
int A;
#endif;
int B;
} example_struct_t;
I am compiling the code using this header file passing CONFIG_A to the GCC with -D option. This way I am able to include A member to the struct or remove if not needed for a given use case.
Now suppose I generate a shared library (.so) and I would like to distribute it. So, I have the .so library and the headers with precompiler directives. The problem is that I would like not to include the -DCONFIG_A in the program using the library, I mean, I would need to hold the options employed at the library compilation time not only in the source files (.c) but also in the header. That is to say, if a compile the library with -DCONFIG_A option I suppose that program using the library shouldn't include that option in compilation time.
Are the precompiled headers the solution for this problem or is there any other alternative (avoiding include a config.h header in every files defining precompiler directives)?
Thank you so much for the guidance.
You can "generate" the code for the structure definition and ship the generated definition alongside the corresponding library. One idea is to keep your structures in a header with no #includes, in which case you can run the C preprocessor on them to get a file with no #ifdefs (which you can then use to build and ship).
Another way is to do something special in your build system. For example CMake has #cmakedefine which you can use inside a C or C++ source file and then generate code from that.
C code on the left, bash window on the right
I am trying to create a new .c file by putting #include "name.h" at the beginning of my file but as you can see I'm getting an error.
It's saying that it cannot find the include file. Am I not linking them together correctly using the bash commands?
You use the right commands, but you need to have a main() function somewhere in your code in order for your program to compile.
Also, you do not need to put #include "name.h" to create a .c (source) file, .h file (headers) are used to declare your functions so you can use them in other source files.
I'm using embedded MATLAB to generate C code from an .m script. However, after the generation I'm left with about 15 files (most of them are source). If I want to write a standalone main in C how do I link to the MATLAB generated code? Is there a way to do this without writing my own Makefile?
So I figured out how to generate static libraries (.a) with the matlab generated code... can I build dynamically loaded libraries (.so)?
I don't know if this is particular to my project, but I find that I can include Matlab generated files the same way as you would normally include anything else.
I copy my generated matlab source files (.c and .h) into my project directly, and then I have to specifically add them to my project. ( I wonder if that has more to do with the IDE I use though).
Then, in main.c that you create, just add the line #include "yourGeneratedFile.h". Within main.c now, you should be able to use whatever functions were created by Matlab.
For instance, in an example:
within main.c, include the header file and use the function you need
#include "SPIUARTDemo30f.h"
//further down in the file
SPIUARTDemo30f_step(); //using the function I asked Matlab to generate
SPIUARTDemo30f.h and SPIUARTDemo30f.c are the generated files from Matlab:
within the .h you'll see
extern void SPIUARTDemo30f_step(void);
and, if you look at the .c you'll find:
void SPIUARTDemo30f_step(void)
{
/* lots of code */
}