Include string generated by C/C++ macro concatenation [duplicate] - c++

This question already has answers here:
Generate include file name in a macro
(3 answers)
Closed 2 years ago.
I am trying to include a header file whose name is version dependent. The concrete name
is given by concatenation of strings with the version number. The last one is retrieved
from CMakeLists.txt using a configuration file.
#include "config.h" # load PROJECT_VER
#define HEADERROOT "foo-"
#define HEADERBASENAME HEADERROOT PROJECT_VER
#define HEADER HEADERBASENAME ".h"
// Equivalent to: #define HEADER "foo-5.1.h"
The string generated is correct, however, it is not possible to include it (appending to the previous statements)
#include HEADER
#include <iostream>
using namespace std;
int main() {
cout << HEADER << endl;
return 0;
}
The error is
main.cpp:6:10: warning: extra tokens at end of #include directive
#include HEADER
^~~~~~
main.cpp:6:16: fatal error: foo-: No such file or directory
#include HEADER
^
compilation terminated.

You can't use a macro to define the filename for an #include statement. This kind of task is better handled in an external build process that creates the necessary source code before the preprocessor/compiler is then invoked.

Related

#include stdio confusion is it needed for each header file?

I know my understanding of #include or how it is compiled is not correct otherwise the code I have would work. I'm confused on why my code needs #include in two locations in order to compile and run correctly.
My main cpp file armperfmon.cpp:
#include "armperfmon.h"
int main(int argc, char* argv[])
{
FILE* pOutFile = NULL;
PrintCounterOptions(pOutFile);
}
main header file armperfmon.h:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "counters.h"
void PrintCounterOptions(FILE* pFile);
second cpp containing function counters.cpp:
void PrintCounterOptions(FILE* pFile)
{
fprintf("print some stuff");
}
second header file for function counters.h
void PrintCounterOptions(FILE* pFile);
The error:
counters.cpp: error: 'FILE' was not declared in this scope
counters.cpp: error: 'pFile' was not declared in this scope
if I enter #include <stdio.h> in the function cpp file then the error goes away and the function compiles/executes as expected. I assumed that in the main.h file when it included <stdio.h> it would be available for use for subsequent FILE* definitions especially because it is included before counters.h is included. As I type this I'm also realizing the more correct include is <cstdio>. If someone could clarify what is wrong with my thought process it would be much appreciated.
It's hard to answer this exactly because you've stripped out all the specific details like filenames but, in brief, C++ source files are compiled independently before the results are linked together, and the "main header" is not seen at all while compiling the "second cpp": it's only the header for your "main cpp file". Indeed, the entire purpose of header files is to serve as a common location for declarations that shall then be #included into multiple translation units, which is what you need to do here by adding the necessary code to your "second header file".

C++ Compiler finds values defined in header multiple times [duplicate]

This question already has answers here:
Header/Include guards don't work?
(6 answers)
Closed 6 years ago.
im searching the internet for about half an hour because of a (actually basic and simple) problem in C++. Maybe im missing something, but I don't know what. Lets say, i have 3 files: "main.cpp", "dosomething.cpp" and "Header.h".
"Header.h":
#pragma once
#ifndef HEADER_H
#define HEADER_H
char text[] = "This is a text";
#endif // !HEADER_H
"main.cpp"
#include <stdio.h>
#include <iostream>
#include "Header.h"
using namespace std;
void main() {
cout << text << endl;
}
and "dosomething.cpp"
#include "Header.h"
void dosth() {
}
Now the compiler/linker tells me that "text" is already defined in another file. Why? I know how guard idioms such as #pragma once and #ifndef etc. work - atleast I think so. I have no idea whats wrong here. The code itself works (when not including the header in "dosomething.cpp").
Any idea?
Edit: Im using Visual Studio 2015
You need to put extern keyword. C/C++ does for every .c/.cpp file will combine the text of files and all definitions will be merged in the linking step. If you write 'extern' before the header variable, you can define it in just one C++ file, and all other files will reuse it. The linker will use just one instance of the variable you externed it to.
So in header.h
#pragma once
extern char text[];
main.cpp remains the same, but"dosomething.cpp" changes slightly
#include "Header.h"
(...)
char text[] = "...";
(...)
https://en.wikipedia.org/wiki/External_variable

C++ Do I have to include standard libraries for every source file?

I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?
Would that be the way to go?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)
Use include guards in your header files
Don't import everything by polluting the global namespace, e.g.
using namespace std;
but rather qualify what you intend to use when you need it
You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)
The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one)
You only need to include the stdafx.h in your .cpp files as the first include.
Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include guards.
myclass.h
#ifndef MYCLASS_H // This is the include guard macro
#define MYCLASS_H
#include <string>
using namespace std;
class MyClass {
private:
string myString;
public:
MyClass(string s) {myString = s;}
string getString(void) {return myString;}
void generate();
}
myclass.cpp
#include <stdafx.h> // VS: Precompiled Header
// Include the header pair
#include "myclass.h" // With this one <string> gets included too
// Other stuff used internally
#include <vector>
#include <iostream>
void MyClass::generate() {
vector<string> myRandomStrings;
...
cout << "Done\n";
}
#endif
Then in main(...), you can just include myclass.h and call generate() function.
The stdafx include should be at the top of every .cpp file and it should NOT be in .h files.
You could put #include < string > in stdafx.h if you don't want to put it in every other file.
I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave
#include <stringLib1.h>
#include <stringLib2.h>
In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.
So, considering a common header file "includes.h".
#ifndef INCLUDES_H
#define INCLUDES_H
#include <string>
#include <stringLib1.h>
#include <stringLib2.h>
/***Header files***/
#endif //INCLUDES_H
This is now your common header file. This you can include in all your project files.

Difficulty with making a header file for C++

I'm trying to make a header file called hippiewh. However, I have been running into some problems.
here's the code for the header file:
#include <iostream>
#include <string>
using namespace std;
#ifndef hippiewh_h
#define hippiewh_h
int askInt(string prompt)
{
int var_int;
cout << prompt;
cin >> var_int;
return var_int;
}
#endif
there are functions like that one for every data type. string, float, and so on.
I can't get this header file working with the online IDE and compiler at Compilr.com
The error i'm getting is:
hippiewh.h: No such file or directory
Andrew, I bet you didn't write the include like this #include "hippiewh.h".
and you did it like this #include <hippiewh.h> which is wrong for your own created header files.
If you put all the code here we could help you
It should work without hassle :/
You must include the file this way
#include "hippiewh.h"

Problem in Include file

I want to write two program(.h and .cpp) with below code and use .h file in .cpp but when i run it in TC occur below error
.h File
#ifndef ADD_H
#define ADD_H
int add(int x, int y)
{
return x + y;
}
#endif
.cpp file
#include <iostream.h>
#include <conio.h>
#include "Add.h"
void main()
{
clrscr();
cout << "Sum of 3 and 4 :" << add(3, 4);
getch();
}
Error
Unable to open include file "Add.h"
There's a few things you should look into:
the location (search path) of header files is implementation dependent, both for the <> and "" variants - make sure your header file is in that path somewhere.
you may find that you need to use add.h (all lowercase).
you shouldn't generally include code in header files (you should put it in a separate C file and just use the header file to list declarations (or the prototype in your case).
if that's Turbo C you're using (and it probably is, given the clrscr and getch), there's really no excuse not to upgrade to a more modern environment.
You probably just need to add -I. flag to you compile line.
Add.h is not in the includes path of your compiler.
By the way, iostream.h is deprecated, you should include iostream. Also, cout is in the std namespace, so you need a using namespace std; in your .cpp file or, alternatively, use std::cout instead of cout.