What is the difference between a cbp file and a cpp file? - c++

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++

Related

Visual C++ build produces an empty .obj while manual compile succeeds

I have a Visual C++ Professional 2019 Version 16.11.8 solution with 4 projects, one of them a GoogleTest project, others are libraries. It worked long just fine, but today it started complaining about 4 missing methods in a class. Those are in a separate .cpp file.
During build, it compiles this file, but the result is only 70 kB (should be 500kB) with no methods in it (examined with objdump). Unresolved references. When I compile the specific file using Ctrl-F7, then issue the build, everything is OK.
I've tried clean all, rebuild all, restart computer, delete all the Debug directories together with .vs, but nothing.
I haven't customized anything on that ,cpp file, at least not on purpose.
What can I do? Thanks in advance.
The cause was some misplaced header and source files. Header in source directory or the opposite way, I don't remember. Making order healed the project. Apparently I wans't careful enough during adding new files.
Make sure, that
Each header file gets in the project include directory.
Each header file is added via the new header template, and gets .h suffix.
Each source file gets in the project src directory.
Each source file is added via the new cpp file template, and gets .cpp suffix.

Xcode 11 header file issue

I'm pretty new to using Xcode with C++ development. For some reason, the compiler isn't treating .h files correctly. It doesn't color code correctly, nor does it highlight compile-time errors. I've tried looking through the settings to see if I can change this, and I found that it treats .h files by default as c-headers. I changed this to C++ header file in one of them to see if this would help, and then closed the project and reopened. Nothing.
Maybe try to rename your files to .hpp ?

No such file or directory in Arduino Project

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.

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).

C++ Header File Are Not Included - C++ Sample Application For Linux

I know it is completely stupid question, but when I received a sample application supposed to run on Linux, I have got some .cpp files, with no .h files, even-though that inside the .cpp file there is a mentioning of some .h files, is it Ok? or the files are really missing and I should ask the supplier -which is a big company- to provide me with those missing .h files??
Sample of the files I have received:
The file name is: XXX.cpp
#include "XXX.h"
XXX::XXX(bool aEnableLogging /*= true*/) :
abcd(aEnableLogging)
{
//SOME CODE
}
XXX::~XXX()
{
//SOME CODE
}
bool XXX::Run()
{
//SOME CODE
}
.
.
.
Another question, can I easily write the .h files manually using .cpp? and what is the header file XXX.h for this XXX.cpp file for example??
PS. I am slightly new to C++, and I am trying to build this application using MS VS2010, can I do this? or I need clips or another Linuxy IDE?
Yes, you need the header files.
You may be able to infer some of what is in them, but you will not be able to fully rewrite them yourself. For example, just by looking at this file we have no idea whether XXX::Run() should be public, private, or protected. It's also possible that there are inline functions/methods or even macros in the header file that don't appear in the source files.