I'm have variable 'filename' with contents 'code.h', i'm need do this,
#include <filename>
why it's not working? Or #include not works with variables?
#include is for other header files not variables. If you want to conditionally include header files you can use precompiler commands:
//config.h
#define USE_HEADER_CODE_H
//other.h
#include <config.h>
#if defined(USE_HEADER_CODE_H)
#include <code.h>
#else
#include <other_code.h>
#endif
Related
I'm using rapidjson, which is an all header library. In rapidjson.h, there is a macro RAPIDJSON_ASSERT, in one of my cpp files, I would like to redefine it, so I have this code at the top of my file:
#include "stdafx.h" // for windows
#pragma push_macro("RAPIDJSON_ASSERT")
#define RAPIDJSON_ASSERT(x) if(!(x)) throw std::logic_error("rapidjson exception");
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
....
....
#pragma pop_macro("RAPIDJSON_ASSERT")
Here is the whay that rapidjson.h defines RAPIDJSON_ASSERT:
#ifndef RAPIDJSON_ASSERT
#include <cassert>
#define RAPIDJSON_ASSERT(x) assert(x)
#endif // RAPIDJSON_ASSERT
The documentation states that to override the RAPIDJSON_ASSERT logic, you just have to define RAPIDJSON_ASSERT before you include any of the files.
The issue is that when I run the code in the debugger, RAPIDJSON_ASSERT is not being redefined. I checked stdafx.h for anything which would include the rapidjson header files, and there isn't anything.
I was under the assumption that each compilation unit should run through the header files.
Note that if I move the redefinition of the macro into stdafx.h I get the macro redefined, but I was hoping to be able to do it per compilation unit.
It seems like you want to change the definition of RAPIDJSON_ASSERT for the rapidjson code itself
If so, you need to add a #define after the place where it is defined. Unless you want to edit the rapidjson.h file, the only alternative is to do this:
#include "stdafx.h" // for windows
// One would assume that the macro gets defined somewhere inside here
#include "rapidjson/rapidjson.h"
// Compiler will complain about macro redefinition without this #undef
#undef RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x) if(!(x)) throw std::logic_error("rapidjson exception");
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
Now the definition of RAPIDJSON_ASSERT is changed for the rest of the header files. You don't need the push_macro and pop_macro shenanigans - macros only are valid for each unit
Note that it's not a a good thing to redefine things for libraries using #define
I have simple project where I use tiny ttmath library for C++ (big nums).
This library consists of 13 *.h files.
I have included all these files in a stupid way:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
That won't work because the preprocessor is not going to expand characters to match things in the way you expect wildcards to work.
My recommendation would be to create a single custom header file of your own, and place all the #include entries in there. For example, in your .c file, you can add your own header:
#include "my_header.h"
And the contents of my_header.h would be:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
Basically, you put everything in a single header, and include that one instead.
The preprocessor doesn't have an "include all" built into it. Neither does it accept wildcards in filenames. You'll have to manually include all of them.
A common solution is to place all the includes in a new .h file and include that one every time you need all of them.
I am trying to compile my code on Linux and created a simple make file.
In one of my headers, there is a compile-time conditional #include for a file that emulates Unix's dirent.h.
I put this include in a #if defined(_WIN32) || defined(_WIN64), and yet g++ happily includes the file, which results in endless errors.
The whole block looks like this:
#if defined(_WIN32) || defined(_WIN64)
#include "dirent.h"
#define strncasecmp _strnicmp
#else
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utime.h>
#endif
Is there any obvious reason for this to happen?
So, given the comments: Don't call your "compatibility header" "dirent.h" but something like "dirent_compat.h" or "dirent_win.h".
Alternatively, don't add current directory to the include path for system includes (you really shouldn't have <...> includes in current directory anyway)
Basically I have lots of errors like these:
IMU/IMU.cpp.o: In function `MPU6050::dmpInitialize()':
Projects/Arduino/libraries/IMU/MPU6050_6Axis_MotionApps20.h:281: multiple definition of `MPU6050::dmpInitialize()'
Quadcopter.cpp.o:Projects/Arduino/libraries/IMU/MPU6050_6Axis_MotionApps20.h:281: first defined here
But im not sure how to solve this. I have lookes into several other similar questions but didnt fint any answer related to this code.
.ino
#include <Wire.h>
#include <IMU.h>
IMU imuController;
void setup() {
Wire.begin();
imuController.init();
}
IMU.h
#include "MPU6050_6Axis_MotionApps20.h"
MPU6050_6Axis_MotionApps20.h
#include "I2Cdev.h"
#include "helper_3dmath.h"
#include "MPU6050.h"
#include <avr/pgmspace.h>
MPU6050.h
#include "I2Cdev.h"
#include <avr/pgmspace.h>
It might be because you header file is included multiple times. What you can do is define guards like this:
#ifndef SOMEVAR - *make sure the file is included only once in the current scope*
#define SOMEVAR
//Symbol definitions
#endif
or you could include #pragma once in your header file, if your compiler supports it.
As W.B suggested, you need include guard for every header file you define.
Something like
Ex: Header.h
#ifndef HEADER_H
#define HEADER_H
// Header stuff in here...
#endif
This is 7 years way too late, but here's what I did
In my own mpu_sensor.h file, I only included
#ifndef MPU_SENSOR_H
#define MPU_SENSOR_H
#include "MPU6050.h"
#include "helper_3dmath.h"
....
#endif
Note that I don't MPU6050_6Axis_MotionApps20, since most datatypes are
In my mpu_sensor.cpp file, here's my includes:
#include "MPU6050_6Axis_MotionApps20.h"
#include "mpu_sensor.h"
Please note that MPU6050_6Axis_MotionApps20.h must be before my including my own header file.
It works now.
I agree that the library itself should be updated, but it seems like the author is not updating for the past few years.
How do you implement GLEE in your code so that it loads extensions used within included files?
For example, I have a windows build environment using cygwin and GCC, and am linking to the libraries for GLEE, GLUT, and opengl32.
The includes in my main file are ..
#include <windows.h>
#include <stdio.h>
#include <GL/GLee.h>
#include <GL/glut.h>
#include "SampleUtils.h"
#include "LineShaders.h"
SampleUtils.h declares methods that utilize OpenGL extensions, such as glCreateShader, which are implemented in SampleUtils.cpp. But when I attempt to build these files, the extensions are undeclared. I've tried a couple of different approaches.
Such as including in SampleUtils
#include <GL/gl.h>
#include <GL/glext.h>
which results in undeclared errors
#include <GL/GLee.h>
which throws a long list of errors that seem to relate to the fact that GLEE has already been included.
I can load the same extensions by implementing these methods in the main file, but can't get them to load from an included file. How is this dealt with?
Look in GLee.h:55
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <GL/gl.h>
#elif defined(__APPLE__) || defined(__APPLE_CC__)
#define GL_GLEXT_LEGACY
#include <OpenGL/gl.h>
#else // GLX
#define __glext_h_ /* prevent glext.h from being included */
#define __glxext_h_ /* prevent glxext.h from being included */
#define GLX_GLXEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glx.h>
#endif
Wherever you #include "GLee.h" you don't have to #include gl.h, glext.h, or windows.h.