I tried to use mbed.h library from this link: https://github.com/ARMmbed/mbed-os/#679d24833acf0a0b5b0d528576bb37c70863bc4e for nucleo F207ZG but it fails during build because of device.h library. I am new in the field so I am not sure how to solve this. I tried to change main.c file to main.cpp and the problem I had with cstddef.h library solved and device.h error appeared. enter image description here
Related
I am trying to compile a simple project which uses one of my headers. I am on Windows and I am using MinGW-W64-builds-4.3.5 Suppose the project is called test.cpp and I want to compile it using my headerosmanip which requires also the linking of its created static library libosmanip.lib. The static library has been compiled and created in MSYS2 and then copied into Windows filesystem. If I try to compile with:
g++ -std=c++17 -losmanip .\test.cpp
To search for the system headers and library path I did:
g++ -v test.cpp
and decided to put headers into C:\MinGW\bin\..\lib\gcc\i686-w64-mingw32\8.1.0\include\c++ and the static library into C:\MinGW\lib\gcc\i686-w64-mingw32\8.1.0\.
I got the following error:
C:/MinGW/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe:
cannot find -losmanip
collect2.exe: error: ld returned 1 exit status
I've tried also by adding the -L option, linking the library path, or editing the LIBRARY_PATH variable ( $Env:LIBRARY_PATH+=";C:/MinGW/bin/../lib/gcc/i686-w64-mingw32/8.1.0/"), but it still doesn't work.
I tried to move the library into the same folder of the test.cpp file and compile, but again the same error occurs.
It's strange because I tried same thing on Ubuntu, MacOS, MSYS2 and Cygwin64 and it works.
Can you help, me please?
I finally solved the issue. The problem was related to the fact the the suffix of my library was .lib. By changing it in .a and rebuilding the library passing the correct static library name to the ar command the problem disappeared.
I am using CodeBlocks 20.03 and I want to use SDL_Image library to load JPEG, JPG and other image formats in my program. For that reason, I installed SDL_2.0.14 from the sdl website as a development library but not the runtime one. Now I copied all the .dll files into the MinGW bin folder and .lib files into the lib folder and again the SDL_image.h header file into SDL2 folder under the CodeBlocks compiler MinGW include folder.
Furthermore, I added the following lines in the linker library line through Settings->Compiler->Linker settings:-lmingw32 -lSDL2main -lSDL2 -lSDL2_image
I am sorry to be unable to post any photos for not having 10 reputations.
And now I am trying to compile the following code in my SDL2 project:
#include <iostream>
#include <SDL2/SDL.h>
#include "SDL2/SDL_image.h"
int main()
{
std::string path{"E:/C++ Project/Project/hrithik.jpg"};
IMG_Load(path.c_str());
IMG_Quit();
return 0;
}
But the wondering part is that my code doesn't compile, showing an error on the IMG_Load(..) line but, not in SDL2/SDL_image.h line.
I am using 64 bit Windows but 32 bit MinGW and SDL. I want to load png file format but without this function, it is not possible. Again, if I create any object of enum IMG_InitFlags, present in that header file the code works and compiles. I checked the header file definition and saw that my parameters were all correct.
I am trying to add a cpp file to arduino project that has the following setup...
project
--folder
--foo.h
--foo.cpp
--project.ino
I have a #include "folder/foo.h at the top of project.ino. However while the header provides the prototype of the function, the function definition is in the cpp file. When I try to compile the code using the Arduino IDE, it fails with error
Undefined reference to 'bar()'
and bar() is located in foo.cpp
I looked at this but I do not have a setting for sketch/import library (however I do have sketch/include library, however I did not see anything close to using a custom folder location)
I looked at this too. But same as above, that setting does not exist in my ide. (Which I downloaded recently)
Code
//project.ino
#include "folder/foo.h"
void setup() {
Serial.begin(9600);
}
void loop() {
bar();
}
//folder/foo.h
#include "Arduino.h"
void bar();
//folder/foo.cpp
#include "foo.h"
void bar() {
Serial.println("bar");
}
Error
/tmp/ccNTRFfU.ltrans0.ltrans.o: In function `loop':
/home/temporary/project/project.ino:9: undefined reference to `bar()'
collect2: error: ld returned 1 exit status
exit status 1
What I would expect to happened is a way to link the cpp folder without having to put all the files in the same root folder of the project.
--Edit 1:
added code
--Edit 2:
added #include "Arduino.h"
--Edit 3:
added Serial.begin(9600);
How to properly include C/C++ headers and source files in your Arduino Project.
This answer has been tested and compiled to ensure it works. (Completed in Linux Ubuntu with the Arduino 1.8.7 IDE).
You have 2 problems.
1st: Arduino's unusual build process (described here) doesn't allow including from sub-folders in your project directory where your .ino file for this project is located.
[UPDATE: THIS ONE MAY HAVE BEEN MY MISTAKE ONLY, NOT YOURS, when I was duplicating your code on my PC: I accidentally used foo.c instead of foo.cpp]
2nd: C++ can only be used inside C++ source files, so you must change foo.c to foo.cpp, since Serial.println() is a C++ call to a C++ class's (Serial's) println() method.
To fix 1, simply change your folder structure to have everything in a single folder:
project
├── foo.cpp
├── foo.hh
└── project.ino
I present an alternate fix for #1 below too.
To fix 2, (this is mandatory!) make foo.c --> foo.cpp and (optionally, but recommended, to show it is a C++ header file) foo.h --> foo.hh. Update your includes in the .ino and .cpp file now too to #include "foo.hh".
That's it! Now close the Arduino IDE, then reopen it and reopen your project, and you'll see the following new tabs show up:
It now compiles just fine!
Learning: how did I figure this out?
First, turn on verbose compilation in the Arduino IDE: File --> Preferences --> check the box for "Show verbose output during 'compilation'".
Now, when you compile, all errors will show up in the bottom of the IDE window, as well as the exact compilation or linking commands which throw the error.
Once I fixed the folder structure, but your files were still C instead of C++ files, I saw this error:
Compiling sketch...
/home/gabriel/Downloads/Install_Files/Arduino/arduino-1.8.7/hardware/tools/avr/bin/avr-gcc -c -g -Os -w -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/home/gabriel/Downloads/Install_Files/Arduino/arduino-1.8.7/hardware/arduino/avr/cores/arduino -I/home/gabriel/Downloads/Install_Files/Arduino/arduino-1.8.7/hardware/arduino/avr/variants/eightanaloginputs /tmp/arduino_build_233569/sketch/foo.c -o /tmp/arduino_build_233569/sketch/foo.c.o
/tmp/arduino_build_233569/sketch/foo.c: In function 'bar':
foo.c:9:5: error: 'Serial' undeclared (first use in this function)
Serial.println("bar");
^
/tmp/arduino_build_233569/sketch/foo.c:9:5: note: each undeclared identifier is reported only once for each function it appears in
exit status 1
'Serial' undeclared (first use in this function)
Notice the file it failed to compile was /tmp/arduino_build_233569/sketch/foo.c, and that the avr-gcc C compiler (rather than the avr-g++ C++ compiler) was in use at the time.
I then opened the /tmp/arduino_build_233569/sketch/foo.c file to examine it and look for anything unusual about it.
Next, I used Eclipse to start tracking down includes, to see where Serial gets pulled in (it should have been obvious to me already what the problem was, but I didn't see it yet). I found the following:
Arduino.h is found in "Arduino/Source/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h". It includes "HardwareSerial.h". This header externs the Serial object:
#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
#define HAVE_HWSERIAL0
#endif
HOWEVER, looking back at Arduino.h you'll see that HardwareSerial.h is ONLY included if you are compiling with C++:
#ifdef __cplusplus <========= This means that the following headers are ONLY included if you are compiling with C++! BOOM! That's when it hit me! You're compiling a C file with the C compiler to access a C++ object. That's not ok. Use the C++ compiler!
#include "WCharacter.h"
#include "WString.h"
#include "HardwareSerial.h"
#include "USBAPI.h"
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
#error "Targets with both UART0 and CDC serial not supported"
#endif
#ifdef __cplusplus means that the headers above are ONLY included if you are compiling with C++! That's when it hit me! You're compiling a C file with the C compiler to access a C++ object. That's not ok. You must use the C++ compiler instead. Do this simply by changing foo.c to foo.cpp. Done.
Alternate fix for your problem #1 (the folder structure):
Find your "Sketchbook location" from Arduino IDE: File --> Preferences. Mine, for example, is /home/gabriel/dev/Arduino/Sketches.
Now, go there and create a "libraries" folder. For me that would now be /home/gabriel/dev/Arduino/Sketches/libraries. Everything inside this folder is now considered an Arduino "library", and can be included. Move foo.h [do NOT use foo.hh in this case] and foo.cpp there, like this:
/home/gabriel/dev/Arduino/Sketches/libraries/foo
├── foo.cpp
└── foo.h <==== NOT foo.hh in this case!
Now close and reopen the Arduino IDE, then go to Sketch --> Include Library --> foo, and it will automatically add the following line for you:
#include <foo.h>
The reason you can't use foo.hh in this case is simply because Arduino is looking for .h files only when you add your library include using the menus in this way. That's a bug as far as I'm concerned, and should probably be reported to the Arduino developers. Feel free to take that on.
Addendum:
16 Apr. 2019:
A google search for "arduino add include path" led me to this: https://forum.arduino.cc/index.php?topic=445230.0, where user #pert says:
In recent versions of the Arduino IDE(including 1.6.10) if you want to include libraries from the sketch folder you need to put them in a src subfolder. For example:
Blink
|_Blink.ino
|_src
|_BlinkLib
|_BlinkLib.h
He then says you can include like this:
#include "src/BlinkLib/BlinkLib.h"
I haven't tried this, but that'd be super useful if it works. Give it a shot and let me know if it works. Be sure to tell us which OS and Arduino IDE version you are using.
See Also:
Additional discussion on Github here: https://github.com/arduino/Arduino/issues/5186.
The official Arduino Library specification here: https://arduino.github.io/arduino-cli/latest/library-specification/.
Open a tab in your project for each file.
You can create a new file or import an existing file into your project.
This way you can use multiple *.ino, *.c, *.cpp and *.h files. I didn't find a way to import a local directory or to configure your project structure.
Place your folder inside libraries folder that is a subdirectory of your sketchbook directory.
Open Arduino IDE (if it is already open, quit it and reopen).
Go to Sketch->Include Library. You will find folder under Contributed Libraries section.
Choose folder and you are good to go.
According to "Sketch specification":
In Arduino IDE 1.6.5-r5 and older, no recursive compilation was done.
In Arduino IDE 1.6.6 - 1.6.9, recursive compilation was done of all subfolders of the sketch folder.
In Arduino IDE 1.6.10 and newer, recursive compilation is limited to the src subfolder of the sketch folder.
So, putting your source code files into 'src' subfolder should solve the problem. Tested in "Arduino IDE" v2.0.3.
How can i add the cplex's library into my netbeans 7.4 cpp project?
I've tried to add inserting all the file path :
" #include "/Users/.../Applications/IBM/ILOG/CPLEX_Studio_Preview1251/cplex/include/ilcplex/ilocplex.h""
but I still have error, the compiler said:
There are unresolved includes inside
"/Users/.../Applications/IBM/ILOG/CPLEX_Studio_Preview1251/cplex/include/ilcplex/ilocplex.h""
Thank you
The correct way is to put in your .h/.cxx code
#include "ilcplex/ilocplex.h"
Then in your project setting you add an include path so you get a flag -I pointing to {somewhere}/cplex/include
This will solve the compilation problem.
But you should also set the library by adding the library cplex1251.lib (check name) and the library path {somewhere}/cplex/lib/vs{number}/{variant}
May be you should have a look at the manual
manual, IBM forum
I'm trying to use libjson within a C++ project and the docs tell me to just "add libjson's source to your project, comment JSON_LIBRARY in the JSONOptions.h file and any C++ compiler should compile it."
Being quite new to C++ and all that, how exactly am I supposed to do that (not using any IDE)? Should I just #include the libjson.h file and that's it? Shouldn't I reference libjson somehow in my call to g++ when compiling my project?
thx in advance
If you go into the libjson library folder, you will see a makefile. Navigate to that directory in a terminal and type:
make
then
make install
Then, in your code
#include <libjson.h>
or, depending on your include path:
#include <libjson/libjson.h>
That should be all that you need to do.
If you need additional help, you can post in the help forum at sourceforge (I am the author of libjson)
You have to:
One,
#include <libjson.h>
in order to get access to the functions and data types the library offers, then
Two, link against the libjsonz library:
g++ -o myprogram myprogram.c -ljson
(the -ljson flag has to come last or you'll get a linker error with never versions of GCC.)
EDIT: if you need to build the library, you typically have a configure script or a Makefile. See how to use them.
if you install json you should find include file at /usr/local/include
so
#include <json/json.h>
gcc exasmple.c -ljson