include own header file in arduino's ide - header-files

Hey there and merry christmas,
I want to include a header file that I created in my main Arduino code. unfortunately upon compilation I get the error message, that the header file could not be found.
I use the Arduino IDE 2.0.3 on windows 10 without major issues so far.
Here is, what i did so far to my best of knowledge and various websites I used as a guide did the steps 1 to 3 apparently working (i.e. compiling | its my first time trying to use my own header file btw):
create working main sketch
create class in main (also working)
create own file (.h) in same folder as main
#include <file.h> and #include "file.h" both produce file not found error
files have read only (rw-r--r--) permission for group and other (seemed sufficient to me)
restarting ide ...
create file.h one directory-level up or down
so, this is my strip down code producing the error and the error message:
filesetup:
../project1/project/
project.ino classfile.h
project.ino:
#include <classfile.h>
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("hello world!");
delay(1000);
}
classfile.h:
#ifndef MY_CLASS_H
#define MY_CLASS_H
#include <Arduino.h>
//usefull class
#endif
Using the Arduino IDE 2.0.3:
Copilation error:
Using board 'nano' from platform in folder: C:\Users\x\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\x\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Detecting libraries used...
"C:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\x\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\eightanaloginputs" "C:\\Users\\x\\AppData\\Local\\Temp\\arduino-sketch-24B7CD866BBD71D5C48AF128C6D936B9\\sketch\\project.ino.cpp" -o nul
Alternatives for classfile.h: []
ResolveLibrary(classfile.h)
-> candidates: []
C:\Users\x\Desktop\project\project.ino:1:10: fatal error: classfile.h: No such file or directory
#include <classfile.h>
^~~~~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: classfile.h: No such file or directory
so please tell me! What do I have to do, to fix my problem?
Thanks for your help and time!

Heyhey,
I found out that the file has to end with a .hpp (the common c++ header file ending that convetion was implemented rather hard in the arduino ide) also the include must use "" instead of <> so:
classfile.hpp can be included using #include "classfile.hpp" for successful validation and compliation of my code.
Hope this will help others too.
If by chance you know if this can be altered in the ide somewhere, please let me know.

Related

'wx/wx.h' file not found when compiling hello world wxWidget program on mac

I have just installed the latest stable version of wxWidget on my mac and tried to compile the code from the hello world tutorial. This is the code that is causing the error:
#include <wx/wxprec.h>'
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
My compiler throws the following warning:
hello_world.cpp:4:10: fatal error: 'wx/wxprec.h' file not found
#include <wx/wxprec.h>
^~~~~~~~~~~~~
1 error generated.
I have checked the directory where I installed wxMac and the file wxprec.h exists in the "include" sub-directory.
If anyone knows why the file cannot be found even though it exists please let me know.
I found out the answer to this problem. To fix it I used the following line to compile/build the hello world app:
g++ `wx-config --cxxflags` -o out *.cpp `wx-config --libs`
after doing this it was only a matter of sorting out the linking, adding header guards and adding in
IMPLEMENT_APP(MyApp)
and
DECLARE_APP(MyApp)
in MyApp.h and MyApp.cpp respectively which functions as the main function.
I got this information from https://wiki.wxwidgets.org/Hello_World

G++ seems to be ignoring #ifdef for an #include

Environment
Ubuntu 16.04
G++ 5.3.1
I have a header file with the following intended to include a different .h file depending on platform:
#ifdef _WIN32
#include "curses.h"
#else
#include <ncurses.h>
#endif
This works fine in windows but in Ubuntu I get errors about the curses.h file:
In file included from /usr/include/unctrl.h:54:0,
from /usr/include/curses.h:1694,
from headers/command_window.h:8,
from command_window.cpp:1:
headers/curses.h:900:19: error: macro "clear" passed 1 arguments, but takes just 0
int clear(void);
This when compiling with:
g++ -g -lncurses -std=c++11 -Iheaders -c -o command_window.o command_window.cpp
Why is headers/curses.h, which is the windows specific file for PDCurses being involved here at all?
/usr/include/unctrl.h contains this line:
#include <curses.h>
And since you've told the compiler to look in your headers/ folder for header files with the -Iheaders flag , the compiler picks up curses.h in that folder.
So you need to drop the -Iheaders flag (and e.g. use #include "headers/header_name.h") or you need to rename your headers/curses.h to not collide with /usr/include/curses.h
In your version of g++, the -I option is not the correct way to add application-specific header files (those that shouldn't be found by #include in system headers) to the search path (this change surprised me as well).
Instead, you should use -iquote headers.
See this answer: How to tell g++ compiler where to search for include files? and this official documentation

include class and compile with g++

Im a beginner in C++ and working with unix. So here is my question.
I`ve written few lines in the main-function, and i needed a function, that is defined in the c_lib - library.
main.cpp:
#include <iostream>
#include "c_lib.cpp"
int main()
{
return 0;
}
i want to execute it on the terminal, so i wrote
g++ -c c_lib.cpp
g++ -c main.cpp
g++ -o run c_lib.o main.o
Until here, there is no error report.
Then
./run
I get the error
error: ./run: No such file or directory
What's wrong?
Including a .cpp is not usually done, usually only headers are included. Headers usually contain the declarations that define the interface to the code in the other .cpp
Can you show us the source of c_lib? That may help.
As the source of c_lib is #included, there is no need to compile it seperately. In fact this can/will cause errors (multiple definitions being the first to come to mind). You should only need to do:
g++ -o run main.cpp
to compile your code in this case.
(When using a header (.h), you will need to compile the implementation (.cpp) seperately)
Compile with warnings turned on:
g++ -Wall -Wextra -o run main.cpp
and you will get more output if there are problems with your code.
Is the run file being output by gcc? You can test by calling ls in the terminal (or ls run to only show the executable if it is present).
If the executable is present, it could be that it isn't marked as runnable. I'll go into that if it is a problem as it is outside the general scope of the site (though still related)
First of all you should not include source file into another source. You should create a header file and put declarations there (that allows main() to call functions from c_lib.cpp or use global variables if any)
When you run g++ you have to look into it's output, if operation succeed or not. In your case it failed so executable run was not created.
Usually you do not call compiler manually but write a makefile and let make utility to call g++.

How to force Eclipse to use g++ instead of gcc?

I already asked how to call a C++ constructor from a C file in How to call a C++ constructor from a C-File. Now when I successfully apply these methods suggested there, I receive an error
fatal error: string: No such file or directory compilation terminated
this error message points to the line: #include <string> in a header of a .cpp file.
I already found out that <string> is used by c++/g++ and <string.h> by c/gcc. Well the problem got clearer, when I checked the console output and there I can see, the (.cpp) file with the error was called by the gcc, which actually expects the <string.h> but that's not my intention - I need to compile it with the g++.
Now my question is: Can I force Eclipse to use a specific compiler? Here, for example just g++ (I heared it is capable of C-code too.) - Or even better, is there a way to chose the compiler for each directory in the workspace ?
Thanks for your advises
Answer respecting the wish of being able to specify the compiler for every subfolder:
What you are searching is probably a makefile project. That allows you to specify the toolchain, being for example the preprocessor, compiler and linker. g++ is an example for such a toolchain, as much as clang++ would be.
You can generate such a project in eclipse, writing the makefiles by hand, or use some build environment, such as CMake, which I would recommend for better portable code.
Both solutions would allow you to specify the compiler, as well as the compile flags, for every single directory of your project, if you wished so.
Writing a makefile for your existing C/C++ project can be achieved by completing the following steps:
in the folder where your source file is, right click and create a new file. New > File
name it makefile and click Finish
The new makefile should pop up in the editor and can be filled like follows:
makefile:
all: executable_name
clean:
-rm main.o executable_name[.exe on windows] executable_name
executable_name: main.o
g++ -g -o executable_name main.o
main.o: main.cpp
g++ -c -g main.cpp
Change Project's Setting can force eclipse to compile using g++:

G++ error from previous declaration, error due to duplicate function name in includes

I am writing a Rcpp code that include two library RcppArmadillo and trng4. However, when I include two header files (RcppArmadillo.h and trng/gamma_dist.hpp) it gives compilation error.
trng/special_functions.hpp:47:39: error: declaration of ‘float lgammaf(float) throw ()’ has a different exception specifier
extern "C" float lgammaf(float) throw();
include-fixed/math.h:476:14: error: from previous declaration ‘float lgammaf(float)’
extern float lgammaf(float);
Full compilation options are
-fopenmp -lgomp -DUSE_R -DNDEBUG -DDISABLE_SINGLE -DNTHROW -DDISABLE_FIO -I/usr/local/include -I"/Users/avi/Library/R/3.0/library/Rcpp/include" -I"/Users/avi/Library/R/3.0/library/RcppArmadillo/include" -fPIC -pipe -std=c++0x -Wall -pedantic -c
Seems like the lgammaf is declared in both header files. I tried using -E with g++ option but that give ld warning " .o, file was built for unsupported file format" and give error function in not available in .Call when I try it to after loading in R. What am I doing wrong?
Perhaps out of context I am using trng4 package to develop a thread gibbs sampler (in openmp) that sample from gamma distribution. I am currently running this MacOS. But eventual it will run in linux server.
It sounds like you do have a problem between Armadillo and trng4. Maybe you should try to, if possible, separate your interface so that you do not need to include from both in the same file.
Someone can correct me if I'm wrong, but I believe you prevent this issue by using #ifndef in each header file so that it's not defined a second time even if it's included a second time. But I guess these aren't your files are they...?
#ifndef __your_unique_header_name__
blah blah
#endif