No such file or directory in Arduino Project - c++

I'm very new in Arduino, so maybe my question will be stupid but I have to ask it!
I made a mqtt client for my nodeMcu chip, and I have this error
/Users/mikevorisis/Downloads/pubsubclient-master/examples/mqtt_esp8266/mqtt_esp8266.ino:27:26:
fatal error: PubSubClient.h: No such file or directory #include
I downloaded the original project from github and I tried to compile the example it has in examples/mqtt_esp8266 but again I have the same problem.
I also tried to put the PubSubClient.h in the same folder but again I have the same problem.
Any ideas?
Thanks in advance.

The file you have downloaded and included in your project is probably not actually a header file. You probably copied the contents of it from github and pasted it into a text document which you saved as a text file with the extension ".h".
It now has the extension "filename.h.txt". The name and extension need to be only "filename.h". Use save as, and select "all files" when saving, and name it "filename.h". Be sure to retype the filename, or it can be auto-filled with the already existing "filename.h.txt" (even if you don's see it!).
If the file now has the right extension, put it in the same folder as your source code file. You can see which directory your source file is in by going to "save as" in your IDE.
A problem you might run into after this is missing definitions. You see, when you use libraries in the form of header files, each header file must usually (in this case, yes) be accompanied by a .cpp file (not necessarily with the same name). The reason for this is that the header file contains declarations, and the cpp file the definitions for said declarations. In other words, the header file is an overview of the facilities available in the library, and the cpp file actually implements the guts of it.
Edit: The example you are trying to run also has #include <ESP8266WiFi.h>, a file that is not available in the github repository that you referred to. I assume that this is a library for a WiFi module or such that you can get elsewhere (manufacturer, other git's or maybe it comes with the Arduino IDE?). In other words, you also need to add its header and (probably) .cpp file to your source directory.

Related

What is the difference between a cbp file and a cpp file?

In code blocks their default file is main.cbp so I usually change it to main.cpp. But there doesn't seem to be a difference between their performances. But then again I just began coding in C++ so I'd like to know if there are any differences before I get too deep.
.cbp is the extension for a codeblocks solution file. Usually the project file will contain the .cpp file. chp files dont contain the actual source code but the procedure for codeblocks to associate files.
In a nutshell, .cpp contains the source code while cbp files dont.
Why do you have to know about that?
Answer: when passing source code, cpp file is the only format that can be opened for IDE other than codeblocks eg. Dev C++

Difference in including the .cpp file and .h file (with the same content in cpp)?

I've recently started learning cpp from basics and was very much confused with the folowing:
Lets say I have a header( test.h which contains only declarations) with some content and some source file (source.cpp) and program produced some result.
If I have copied the same content of that header file to a .cpp file (testcpp.cpp) and included this in source.cpp
In this case, I did not understood what difference it makes?
(I'll not include this testcpp.cpp in make file)
I have seen some threads similar to this but couldn't get a clear idea!!!
I learnt the usage of header and cpp files and have used it correctly in projects till now, Please answer specific to this scenario (I know doing this way adds confusion but just want to know). Will there be any difference doing so or it's just a common practice everyone follows ?
what difference it makes?
The extension of a header file has no effect on anything. You could have just as well named the file test.mpg, .test or just test (changing the include directive obviously), and it would have worked just as well. The extension is for the benefit of the programmer, not the toolchain.
However, it is a bad idea to name it anything other than .h, .hpp or whatever is your convention. If you name it .mpg, people will think that it is a video, and not realising that it is a header file, try to play it in a media player. If you name it .cpp, people will think that it is a source file and may attempt to compile it or maybe add definitions into it.
Including a file with the preprocessor is technically just copying contents of one file into another. Nothing more and nothing less. Everything else about them is just convention.
In makefile, when specifying source file, Can I give my source files with any extension(.fsfs, .xxx) rather than .cpp extension
Technically yes, however compilers usually use the source file extension to detect the language which they will fail to do in this case, so you would have to specify it explicitly.
It changes nothing. It's just a convention whether you use a *.h or *.cpp or *.asdasd suffix, as long as it doesn't get compiled by itself.
Some projects use the .hxx extension for header files and .cc for source file.
Please, for the good of fellow programmers you'll work with, stick to common conventions and don't put header code in .cpp files.
#include just does a copy-n-paste of the file you include into the current file. What the file is named doesn't matter one bit - you can name it "foo.exe" if you like; as long as it contains valid source-code in the context where it is included all is well (but please don't use unconventional names, you'll just confuse people).

netbeans can't find C++ header file in same directory

In one particular cpp file (abc.cpp), when I ask to navigate "to Declaration/Definition", it says "cannot open element "abc.h"". This functionality works for other header files. This in itself is not a big problem, but it also means that auto-complete and syntax highlighting doesn't work for this file.
Some extra info:
The header file is in the same directory as the cpp file and both are included in the active netbeans project
I was able to enter the header file name with auto-complete, i.e. #include "ab<ctrl-space>"
clicking the "Go to header/source" button works both ways for this cpp/h pair.
right-clicking on the class name in the header file, and then selecting "go to source" brings me to the cpp file, as usual.
in other cpp files the connection to the header file is working fine, as is autocomplete & syntax highlighting
netbeans has a green square in the top right of the header file window, indicating "no errors"
I have tried deleting my cache as explained here
I'm using netbeans v8.0.2 on OpenSuse 13.2
Here are some ideas:
Sometimes the Code Assistance is not as good for projects that Netbeans didn't create from scratch. If it is not a complicated Makefile, it might be worth it to create a new project with the C/C++ Application type and copy over and then add each source and header file.
The code assistance depends on analyzing he log from the build each time, so sometimes just rebuilding the project will fix the code assistance.
There are a number of options if you right click the project under the code assistance sub-menu.
Edit the Makefile to make sure this file is being compiled in the same way as the other files that work. It may be getting compiled with different options because it was added later and therefore not providing the same info for code assistance. You will need to rebuild after making these changes for them to have an effect.

Visual Studio C++ able to compile with compile errors (red underlines)

I am having a problem of getting compile errors (red underlines) like:
Error: cannot open source file "stdafx.h"
Here an edited screenshot of the environment:
On the LEFT is my Visual Studio Solution Directory list with the "Show All Files" off.
I am working on a school project, and each Folder are the source files of different parts of the project with different people who are in-charge of them.
For example, Student A and B are incharge of AST and PARSER folders (we will call them sub-projects).
We have an API for each sub-project so other sub-projects know what to call.
At the TOP-CENTER, we have my Source File for a class QueryProcessor. (just the first few lines)
Below it, is the Output for the Build Success.
The red lines are all over all the classes, mainly cause the #include "stdafx.h" cannot be opened by the environment.
On the RIGHT, that is the stdafx.h where we include all the different sub-projects so we save the trouble of each project having a different stdafx.h
However, I am able to build the project. I am pretty sure I am doing this directory/linking wrongly.
This should work
Right click on the solution file
Click Open in Windows Explorer
Find file stdfx.h in explorer and copy the path of the folder
In visual studio solution explorer, Right click on the project file
Click properties-> C/C++ -> General
In the Additional Include Directories paste the path
Combining folders and virtual folders in VC is from my point of view messy because the virtual folders indicate that all files are in one directory and the folders created on the harddrive obviously indicate that all files are in different directories. You can combine it if you know what's going on but in your case I would not recommend it.
I assume you missunderstand the purpose of stdafx.h The purpose of this header file is NOT to put all header filles into it and then just include it to all other files. Here is a SO question about this Purpose of stdafx.h
After cleaning up your stdafx.h file include as many header files into your .cpp files and only put these includes in your header files if they are required in the header file
Turn on show all files, now you will work with actual folders and you can be sure that if you adress a folder like "PKB" that this folder really exists since you can see it in the left solution explorer.
If you use using namespace std; for example make sure you also include the required header files. You might think "hey I already included e.g. iostream in another header file which I now include in this header file so I don't need it" That will really destroy you when you work with bigger projects.
Oh and regarding the stdafx.h include problem as soon as you switch to show all files I assume you will realise that stdafx is in a different file than the file where you use the include. Maybe something like #include "..\stdafx.h" is required (depending on your structure).
I think it's obivious but if you include a header file the include is allway relative to the file which is including the other header file.
stdafx.h is commonly used for creating a precompiled-header, which essentially is a compile-time optimisation such that the compiler will not continually compile these headers for every compilation unit.
If any of these headers changes, you will need to do a full system rebuild.
In reality it is preferable only to use it to include standard headers plus third-party headers (like boost libraries and similar) that you are not ever going to change.
You may decide that some of your own libraries are "set in stone" and can also be included.
Every project, i.e. every part of the project that is built into a separate unit (DLL or .exe) should have its own precompiled header and its own version of stdafx.h
Projects should only ever include their own .stdafx and not those of other projects, therefore this header file can also be used to define your dllexport macro.
When arranging your project headers you should be aware of:
1. Which headers are included externally
2. Which headers are only included internally, and are not even included indirectly externally.
The latter sort should include your stdafx.h file and should ideally not be in the same directory as those headers included from outside your project.

Managed C++ - ignored Header file

So I’ve got a HEADER(.h) and a SOURCE(.cpp) file in a Managed C++ VS2010 solution.
My solution builds successfully even though I added garbage to the header.
public:
someConstructor(); // assume the syntax is good here
asdf // garbage
Why I did this
My original problem was that I couldn't update my CPP file to add a new constructor. When I added a new constructor to my CPP file, my solution failed to build due to a non-existent overridden member function, i.e. my new constructor. I added garbage to the HEADER file to make sure that VS2010 was actually reading my HEADER file. Obviously it was not.
How can I make sure that VS2010 looks at my HEADER file? It seems to be looking at another HEADER file...
Thanks
If you select the file(s) in question and open up the properties window (View->Properties Window) you can see if your project is looking at the correct file or not. Additionally, if the file is not being included in the build (even though it's in the solution), you can set Include in Project to true.
Note that I'm assuming you're using MSBuild. If you're using some other build system, there may be a different procedure for ensuring the build is using the correct header file.