#include inside an x-macro - c++

I want to write an x-macro that generates some code. The code depends on several headers and is intended to be generated inside namespaces.
The problem is that the xmacro's includes are being included inside the namespaces of the caller. Is there any way I can fix this?
Example:
xmacro.hpp:
#include "foo.hpp"
struct bar {
BODY
};
#undef BODY
main.hpp:
namespace ns {
#define BODY int func();
#include "xmacro.hpp" // inserting foo.hpp inside namespace ns
}

Unfortunately no, because X-macros, while being unique, are still ultimately just included files. This is no different from putting #include <iostream> into your own namespace.
X-macro includes should really not do anything but contain the target macro (which has a definition yet to be determined). If the use of your X-macro has prerequisites, I would do something like this:
xmacro_prelude.hpp:
#ifndef XMACRO_PRELUDE_INCLUDED
#define XMACRO_PRELUDE_INCLUDED
#include "foo.hpp"
#endif
xmacro.hpp (usually suffixed with .def, by the way):
#ifndef XMACRO_PRELUDE_INCLUDED
#error "You must include xmacro_prelude.hpp prior to using this X-macro."
#endif
struct bar {
BODY
};
#undef BODY
main.hpp:
#include "xmacro_prelude.hpp"
namespace ns {
#define BODY int func();
#include "xmacro.hpp"
}

Related

Smarter include guard in C++ to include headers several times inside different namespaces

I need a way to do better #include guarding in C++.
When the same header is included twice, the second #include should be ignored (which is simple thing to do):
#include "header1.hpp"
#include "header2.hpp"
#include "header1.hpp" //Should be ignored
But when the same header is included inside nested namespace, then it should be included again (but not more than once per namespace):
#include "header1.hpp"
#include "header2.hpp"
namespace foo_namespace {
//May be this one is needed?
#define NAMESPACE_ID foo_namespace
#include "header1.hpp" //Should be included again
#include "header1.hpp" //Should be ignored
#undef NAMESPACE_ID
};
The question is: how should I guard the code inside header1.hpp?
Additional requirement is that guard itself should be reusable (defined as a macro), since I have lots of headers that should be guarded this way.
A decent solution would be to have a version of the header without guards:
// header_noguard.hpp
// the declarations ...
// header.hpp
#pragma once // or a macro of your choice
#include "header_noguard.hpp"
// header_namespace.hpp
#pragma once // or a macro of your choice
namespace foo_namespace {
#include "header_noguard.hpp"
};
Now, you can include header.hpp and header_namespace.hpp multiple times. Each is guarded against multiple inclusion, but both contain the declarations in their respective namespaces.

How do I solve this error: include files are nested too deeply

Should I put also my headers, and not only the classes inside the ifndef, define etc?
For example I have, this is just an example of code. Now, my question is, should I put the #include "myfile.h" in the ifndef, or should it stay outside?
#include <iostream>
#include "myfile.h"
#ifndef ANOTHERFILE_H
#define ANOTHERFILE_H
struct node
{
int val;
node*next;
}
#endif //ANOTHERFILE_H

Global variable error C++

I'm having an error I don't know how to fix in my large Operating Systems homework. The error I'm getting is "42 duplicate symbols for architecture x86_64". I presume this is to do with my global variables file "global.h". I have 3 global variables I use and "global.h" is included in an abstract class called "PageReplacementAlgorithm.cpp". I have around 6 classes that are derived from the PageReplacementAlgorithm class and they utilize these global variables. I think the problem comes in when I include all these derived classes in my "main.cpp" as I need to make new instances of them. How can I fix the implementation of the global variables?
Global.h
#include "PageTableEntry.h"
using namespace std;
#ifndef Global_H
#define Global_H
extern PageTableEntry pageTable[64];
extern int* frameTable;
extern int framesCount;
#endif
PageReplacementAlgorithm.h
#include "Global.h"
using namespace std;
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
class PageReplacementAlgorithm {
public:
virtual int selectFrame(PageTableEntry &p) = 0;
};
#endif
Example Derived Class (FIFO)
include "PageReplacementAlgorithm.h"
using namespace std;
#ifndef FIFO_H
#define FIFO_H
class FIFO : public PageReplacementAlgorithm {
public:
FIFO();
int selectFrame(PageTableEntry &p);
private:
int entries;
};
#endif
Main.cpp
#include "Aging.cpp"
#include "Clock.cpp"
#include "FIFO.cpp"
#include "MMU.cpp"
#include "NRU.cpp"
#include "Random.cpp"
#include "SecondChance.cpp"
Why do you include all cpp files in main.cpp? I think they contain same includes, right? Even you have the guards there, you do additional includes before that guards and that is probably the source of problems. The main.cpp could contain just main() function and import headers of your classes, there is no need to include cpp.
Also, you can modify your header files to look like this (for sake of extreme safety):
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
#include "Global.h"
using namespace std;
...
#endif
I recommend you to look at answer C++ #include guards
If you get rid of #include "(anything).cpp, things should work much better. When you build the project, or run the compiler e.g. g++ main.cpp foo.cpp, that's when those .cpp files get built and linked into your program.

g++ how to solve warning "used but never defined"? (not static or inline)

I have come across a strange compile error that I cannot make sense of. Firstly the error refers to the function as if it was in an anonymous namespace, however it is in fact inside namespace database. Secondly the "used but never defined" statement suggests that the compile requires me to define the function from within the header. The function is in fact declared in a separate implementation file. However the function is neither static nor inline so I am at a loss as to why it requires a definition in the header. It is a requirement that this piece of code is strictly compliant, because of this I have compiled with both -Wall -Werror. I have also included a shortened version of my source code for clarification.
Note: This question is different from other similar questions asked here in that it does not involve static or inline functions.
Error:
In file included from src/main.cpp:6:0:
include/database.hpp:19:6: error: 'void {anonymous}::SetupSettings()' used but never defined [-Werror]
void SetupSettings();
^
cc1plus.exe: all warnings being treated as errors
main.cpp
#include <iostream>
#include "config.hpp"
#include "database.hpp"
int main() {
database::SetupSettings();
return 0;
}
database.hpp
#ifndef database
#define database
#include <iostream>
#include "config.hpp"
#include "sqlite/sqlite3.h"
namespace database {
extern sqlite3* settings_database;
void SetupSettings();
// ^^ Apparent warning here.
} // namespace database
#endif
database.cpp:
#include <iostream>
#include <vector>
#include "config.hpp"
#include "database.hpp"
#include "sqlite/sqlite3.h"
namespace database {
sqlite3* settings_database;
void SetupSettings() {/*More code here*/}
} // namespace database
The problem is caused by use of:
#ifndef database
#define database
After that,
namespace database { ...
is seen as
namespace { ...
i.e. an anonymous namespace.
You need to use a different include guard macro, such as:
#ifndef database_hpp
#define database_hpp

C++: Strange header error showing up

#ifndef _MY_OPENCLPLATFORM_
#define _MY_OPENCLPLATFORM_
#include "OpenCL.h"
namespace my
{
class OpenCLPlatform
{
cl_platform_id mplatformID;
cl_uint mnumDevices;
std::vector<OpenCLDevice> mdevices; // OpenCLDevice was not declared in this scope
public:
OpenCLPlatform(cl_platform_id platformID);
void getDevices();
void printInfo();
cl_platform_id& getPlatformID();
};
}
#endif
#ifndef _MY_OPENCLDEVICE_
#define _MY_OPENCLDEVICE_
#include "OpenCL.h"
namespace my
{
class OpenCLDevice
{
cl_device_id mdeviceID;
public:
OpenCLDevice(cl_device_id device);
void printInfo();
void printDeviceType(cl_device_type deviceType);
};
}
#endif
#ifndef _MY_OPENCL_
#define _MY_OPENCL_
#if defined(__APPLE__) || defined(MACOSX)
#include <OpenCL/opencl.h> // This works only for XCODE compiler
#else
#include <CL/cl.h>
#endif
#include <cassert>
#include <iostream>
#include <vector>
#include "Exception.h"
#include "OpenCLDevice.h"
#include "OpenCLPlatform.h"
namespace my {
class OpenCLDevice;
class OpenCLPlatform;
class OpenCL;
class OpenCL
{
cl_uint mnumPlatforms;
std::vector<OpenCLPlatform> mplatforms;
void getPlatforms();
public:
OpenCL();
~OpenCL();
void quickSetup();
void printPlatformVersions();
};
}
#endif
Does the the ordering "class OpenCLDevice; class OpenCLPlatform; class OpenCL;" matter? Sometimes, header files depend on each other which can lead to "hard to follow" or convoluted inclusions...Do you have a "one way" technique to deal with convoluted inclusions that you use all the time?
Edit:
I changed the code to match my real problem. If you look at the code above, the compiler is saying that 'OpenCLDevice was not declared in this scope'.
Edit:
I finally got the code to work, and this is what I did:
1. add #include "OpenCLDevice.h"in OpenCLPlatform.h
2. compile
3. remove #include "OpenCLDevice.h"in OpenCLPlatform.h
4. compile
It works now!
Edit:
I cleaned the project and removed all dependencies, and I'm getting the same errors again.
Edit:
I think compiler did something to the code. It may have chose to not include libraries that aren't used in the header and source file, but are used in other headers and source codes
Since you are including classa.h and classb.h where both classes are (presumably) defined, you shouldn't even need the forward declaration.
However, if you did not include them, then no, order of the declarations wouldn't matter. As long as as a class is forward declared before it is used you should be OK.
I see two potential issues:
Your #include "OpenCL.h" may not include the file you expect (yours), but instead some system file.
Forward declarations can't be used in your case. It works only when you have pointers or references to class instances. Your vector<OpenCLPlatform> requires the class declaration (i.e. inclusion of the corresponding header).