Is it possible to declare static variable in a #define directive?
// header file
#define TEXT_ENUM
#ifdef TEXT_ENUM
#define TEXT_HANDLING_MACRO \
static const char * TEXT[]; \
static const char * getText( int _enum ) { \
return TEXT[_enum]; \
}
#else
#define TEXT_HANDLING_MACRO
#endif
struct Foo {
TEXT_HANDLING_MACRO
};
// cpp file
#include "foo.h"
const char * Foo::TEXT[] = {
"ONE",
"TWO",
"THREE",
0
};
How compiler will resolve static const char *, when I include this header file in some other file and try to access Foo::TEXT[].
Is it possible to declare static variable in a #define directive?
Yes, it is possible.
How compiler will resolve static const char *, when I include this header file in some other file and try to access Foo::TEXT[].
It's resolved at the linking stage.
You have to figure out what happens through the stages of compilations that the C compiler uses.
Since #define si a pre-compiler directive, everything will be resolved before actually compiling or looking at the code. Pieces of text (code, functions, whatever is included) will be passed on or filtered according to the directive.
All the rest happens after that, like compilation, that will look for global variable declaration, and linking, that will look for the address of those variable.
In you case, if you take your file and compile with a gcc compiler and the -E option (to do just the precompiler stage), you will get:
struct Foo {
static const char * TEXT[]; static const char * getText( int _enum ) { return TEXT[_enum]; }
};
const char * Foo::TEXT[] = {
"ONE",
"TWO",
"THREE",
0
};
Related
I'm doing functional testing on c code. Have include .c file in test .cpp file so far, everything works well. But I need to include the same .c file in another .cpp file where I do other unit tests. Then I get problems that is already defined. Because I already include it in the first file cpp.
If merge all the test methods into one cpp file then it works well. If keep all unit test in one file get to big handle so I need to split up diffrent files in same project it also create help functions for secure that functions get in capsules.
This not a normal LNK2005 because I can not declare the variable and functions in .h: as extern BOOL MyBool; and then assign to it in a .c or .cpp file. Because need include c file as I do unit test this function. Also I can't or should avoid do any changes .c.
I looking way to keep include of .c local not effect other files in same project.
source.h
#ifndef SOURCE_H_
#define SOURCE_H_
#include "../car.h"
enum INITIALMODE {
INITIALMODE_NOT_POSITIONING, // 0
INITIALMODE_NO_DRIVER_INPUT, // 1
INITIALMODE_POSITION_LOW_POSITION, // 2
INITIALMODE_POSITION_STANDARD_POSITION, // 3
INITIALMODE_POSITION_HIGH_POSITION // 4
};
void initMotor(void);
#endif
source.c
/* Compiler include files */
#pragma once
#include "positioning.h"
#include "api.h"
#include "drive.h"
#include "types.h"
static void updateTarget(void);
static SWord getMax(UWord Limit, UWord Aux);
static DWord getHeight(void);
static Bool isMode(void);
static Bool isExiting(void);
#define cMaxHeight 100 * Profile.s.Max /* m -> mm */
void initMotor(void)
{
// do something
}
static void updatePositioning(void)
{
// do something
}
Test files look like this, however, this is very scaled for making exemple small.
UnitTest.cpp and UnitTest2.cpp
#include "CppUnitTest.h"
#ifndef UNIT_TEST
#define UNIT_TEST
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Test
{
extern "C" {
#include "../../Test/source.h"
#include "../../Test/source.c"
}
TEST_CLASS(UnitTest_1)
{
public:
TEST_METHOD(Test_1)
{
// Verify that the initial state is as expected.
initTest();
//Expected value
UWord Expected = 500
//Trigger to execute.
UWord Test = updatePositioning();
// Verify that the results are as expected.
Assert::AreEqual(Expected, Test);
}
};
}
#endif
You should never include a .C or .CPP file.
However, you can compile C code in C++. Here's an example based on the information you gave in your initial post.
YourCCode.h
#ifndef YourCCode_H
#define YourCCode_H
int FunctionToTest(int SomeParams);
int TestStaticFunctions(int SomeParam1, int SomeParam2);
#endif // YourCCode_H
YourCCode.c
#include "YourCCode.h"
static int SomeStaticFunction(int Param1, int Param2)
{
return Param1 + Param2; // that needs extensive testing, obviously.
}
int FunctionToTest(int SomeParams)
{
return SomeStaticFunction(SomeParams, 1);
}
int TestStaticFunctions(int SomeParam1, int SomeParam2)
{
return SomeStaticFunction(SomeParam1, SomeParam2);
}
UnitTest1.cpp
extern "C" {
#include "YourCCode.h"
}
bool TestFunction(int Value)
{
return (FunctionToTest(Value) == Value+1);
}
UnitTest2.cpp
extern "C" {
#include "YourCCode.h"
}
void AnotherTestFunction(int Val, int Val2)
{
int newValue = TestStaticFunctions(Val, Val2);
ASSERT(newValue == Val+Val2);
}
Then compile your CPP and C file.
After you clarified your intent, I realized you're trying to test static functions from another unit. Static function, by definition, are only available to other functions in the same translation unit. This is mostly use as a protection to prevent programmers to call some functions without knowing how to validate their inputs, know the call order, etc...
My best bet here is either you choose that your functions are not static and you can test them from outside your translation unit, or you implement the test functions inside the translation unit containing those static functions. I would recommend the latter as a translation unit should know (architecturally speaking) how to test their own features.
As a third solution, if you don't have any control over the content of the C file (but since you have that C file, I doubt it), you could have a proxy CPP file that includes the C file, and create a proxy call for each static call.
That is a ugly hack, though, and could break very easily if the C file ever gets updated, so I'm advising against it.
Here's a quick example :
YourCCode.h
#ifndef YourCCode_H
#define YourCCode_H
void SomeFunction(void);
#endif // YourCCode_H
YourCCode.c
#include "YourCCode.h"
static int AddSomething(int Param1, int Param2)
{
return Param1 + Param2;
}
static int SubtractSomething(int Param1, int Param2)
{
return Param1 - Param2;
}
void SomeFunction(void)
{
// code meant to be called externally.
}
ProxyTestCode.hpp
bool TestAddSomething(void);
bool TestSubtractSomething(void);
ProxyTestCode.cpp
extern "C" {
#include "YourCCode.h"
#include "YourCCode.c"
}
bool TestAddSomething(void)
{
return (AddSomething(2,2) == 4);
}
bool TestSubtractSomething(void)
{
return (AddSomething(2,2) == 0);
}
UnitTest1.cpp
#include "ProxyTestCode.hpp"
void TestAdd(void)
{
ASSERT(TestAddSomething());
}
UnitTest2.cpp
#include "ProxyTestCode.hpp"
void TestSubtract(void)
{
ASSERT(TestSubtractSomething());
}
If you do that, don't compile your C file in your project.
I defined a function to show a message when debug flags are off in a header file as below:
#ifdef NDEBUG
#define debug_msg(expr, msg) (static_cast<void>(0))
#else /* Not NDEBUG. */
#ifndef SHOW_DEBUG_H_
#define SHOW_DEBUG_H_
#include <stdio.h>
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
#endif
#define debug_msg(expr, msg) \
((expr) \
? _show_in_debug(__FILE__, __LINE__, __func__, msg) \
: static_cast<void>(0))
#endif
when I include the header in more than a file, I get the following error:
multiple definition of `_show_in_debug(char const*, unsigned int, char
const*, char const*)'
I don't exactly know what I am doing wrong here, any help ?
Even with the include guards, you end up with a definition of _show_in_debug in each compilation unit. Linking those units then results to a multiple definition error.
For a debugging function like this, define the function as static so that it is not visible outside its compilation unit:
static void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
If you include that header into more than one .c file, each of them will define the function. That is what the error says.
What you should do is to only declare the function in the header (i.e. only put the prototype there; which you always should) and then define it in a single .c file (i.e. put the body in the .c file), encapsulated into the same switches as the prototype.
Header to be changed to this:
/* ... as you quoted ... */
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg);
/* ... rest of what you quoted ... */
Code file to contain this:
#incude <stdio.h>
#include "Yourheader.h"
#ifdef NDEBUG
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
#endif
I assume the file header file that you have presented is included in several source files. Why is this a problem? That is because you have defined your _show_in_debug() function in the header instead of declaring it in the header and defining it in a source file file. This leads to the function be defined in multiple source files that have included your header.
See http://www.cprogramming.com/declare_vs_define.html for more details (especially, see the "Common Cases" section).
Consider following header file,
// filename : packet_types.h
#ifndef _PACKET_TYPES_
#define _PACKET_TYPES_
struct Packet {
enum Type {
typ_ONE,
typ_TWO,
typ_THREE,
};
static const char * STRINGS[] = {
"ONE",
"TWO",
"THREE"
};
const char * getText( int enum ) {
return STRINGS[enum];
}
};
#endif
As Arduino has limited memory, I don't want to include this portion of code when I include this file, packet_types.h,
static const char * STRINGS[] = {
"ONE",
"TWO",
"THREE"
};
const char * getText( int enum ) {
return STRINGS[enum];
}
But for my GUI application, I want the complete file. I want to use same file for both Arduino and GUI, but how can I add compiler directive #define,#ifdef, #ifndef... to do this job, when I include this file from some other file(main.cpp). Thanks.
Although it is possible to use some preprocessor juggling, the right solution is to simply use C++ as it was intended to be used. Only declare these class members and methods in the header file:
static const char * STRINGS[];
const char * getText( int enum );
And then define them in one of the source files:
const char * Packet::STRINGS[] = {
"ONE",
"TWO",
"THREE"
};
const char * Packet::getText( int enum ) {
return STRINGS[enum];
}
With the aforementioned preprocessor juggling, all it ends up accomplishing is the same logical result, but in a more confusing and roundabout way.
I'm attempting to compile my code with g++, but its throwing this compiler error:
Enrollment.h:3:7: error: redefinition of class sict::Enrollment
Enrollment.h:3:7: error: previous definition of class sict::Enrollment
my Enrollment.h:
namespace sict{
class Enrollment{
private:
char _name[31];
char _code[11];
int _year;
int _semester;
int _slot;
bool _enrolled;
public:
Enrollment(const char* name , const char* code, int year, int semester , int time );
Enrollment();
void set(const char* , const char* , int ,int, int , bool = false );
void display(bool nameOnly = false)const;
bool valid()const;
void setEmpty();
bool isEnrolled() const;
bool hasConflict(const Enrollment &other) const;
};
}
Any way to fix this?
The problem is likely that your header file is included (directly and indirectly) in the same translation unit. You should use some way of avoiding the multiple includes of the same header file in your cpp's. I prefer #pragma once in the beginning of your header file - it is not standard but it is supported by all major compilers. Otherwise you can go for good-old include guards:
#ifndef _Enrollment_h_
#define _Enrollment_h_
// Your header contents here
#endif
or with pragma:
#pragma once
// Your header contents here
You need to use some include guards. Either #pragma once or:
#ifndef MY_FILE_H
#define MY_FILE_H
....
#endif //MY_FILE_H
This is to prevent the same code being included in every file where you include this header (double inclusion). This will essentially help out the pre-processor. More information here.
I have global variables that I define in the Utility namespace. This utility is included in several files and it looks like this:
#ifndef _UT_
#define _UT_
namespace UT {
std::string PLATFORM_LINUX_NAME = "linux";
std::string PLATFORM_MACOSX_NAME = "macosx";
std::string PLATFORM_WINDOWS_NAME = "windows";
#if defined(OS_WIN)
int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
#elif defined(OS_LINUX)
int PLATFORM = PLATFORM_LINUX;
#elif defined(OS_APPLE)
int PLATFORM = PLATFORM_MACOSX;
#endif
};
When I include this file in for example files A.h and B.h and C.h, I'm getting a compiler warning that says:
warning LNK4006: "int UT::PLATFORM" (?PLATFORM#UT##3HA) already defined in A.obj; second definition ignored
warning LNK4006: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > UT::PLATFORM_LINUX_NAME" (?PLATFORM_LINUX_NAME#UT##3V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A) already defined in A.obj; second definition ignored
What is the best way that does not involve creating a class for solving this?
Or is creating a UT class the only way?
Define the variables in a single .cpp file and declare them in the .h file. In UT.h:
namespace UT
{
extern const std::string PLATFORM_LINUX_NAME;
extern const std::string PLATFORM_MACOS_NAME;
extern const std::string PLATFORM_WINDOWS_NAME;
extern const int PLATFORM;
}
in UT.cpp:
namespace UT
{
const std::string PLATFORM_LINUX_NAME = "linux";
const std::string PLATFORM_MACOS_NAME = "macosx";
const std::string PLATFORM_WINDOWS_NAME = "windows";
#if defined(OS_WIN)
const int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
#elif defined(OS_LINUX)
const int PLATFORM = PLATFORM_LINUX;
#elif defined(OS_APPLE)
const int PLATFORM = PLATFORM_MACOSX;
#endif
}
I added const qualifier as these appear to be constant values.
One solution is to make them all static, in which case each object file gets its own copy. Another possibility is to only put the declaration in the header and put the definitions in a separate file.
Try
#ifndef _INCL_GUARD
#define _INCL_GUARD
std::string PLATFORM_LINUX_NAME = "linux";
std::string PLATFORM_MACOSX_NAME = "macosx";
std::string PLATFORM_WINDOWS_NAME = "windows";
#if defined(OS_WIN)
int PLATFORM = OSTYPE::PLATFORM_WINDOWS;
#elif defined(OS_LINUX)
int PLATFORM = PLATFORM_LINUX;
#elif defined(OS_APPLE)
int PLATFORM = PLATFORM_MACOSX;
#endif
#endif