6
голосов
3ответов
8082 просмотров

C macro : turn a number into a string

I have a table that defines symbols appearance on a 5x7 dot display. Something like: extern UINT8 symbols[][5] = { {0x0,0x0,0x0,0x0,0x0}, {0x0,0x0,0x5F,0x0,0x0}, {0x0,0x7,0x0,0x7,0x0}, {0x14,0x7F,0x14,0x7F,0x14}, // etc. The leading part of the table matches ASCII table, foll...

8
голосов
5ответов
4688 просмотров

How do I write a cpp __DIR__ macro, similar to __FILE__

The __FILE__ and __LINE__ macros are built into the C Pre-Processor, and are often used for printing debug output with file names and line numbers. I need something similar, but with just the name of the directory at the end of the path. For instance if my code is in: /home/davidc/some/path/to/so...

3
голосов
8ответов
1931 просмотров

Make a cosine table with the gcc preprocessor

I wish to make a cosine table at compile time. Is there a way to do this without hard coding anything?

1
голосов
4ответов
1943 просмотров

Custom C++ Preprocessor / Typeful Macros

Having seen the advantages of metaprogramming in Ruby and Python, but being bound to lower-level languages like C++ and C for actual work, I'm thinking of manners by which to combine the two. One instance comes in the simple problem for sorting lists of arbitrary structures/classes. For instance:...

0
голосов
1ответов
394 просмотров

Preprocessor definitions going haywire. int define redefinition?

I am trying to add a preprocessor definition so that a value is only defined while a certain project is building, then it becomes undefined. I have gone into my project properties -> preprocessor -> preprocessor definitions. In here, I typed #define PROJECTNAME_EXPORT, in hopes that I cou...

127
голосов
14ответов
111353 просмотров

How do I show the value of a #define at compile-time?

I am trying to figure out what version of Boost my code thinks it's using. I want to do something like this: #error BOOST_VERSION but the preprocessor does not expand BOOST_VERSION. I know I could print it out at run-time from the program, and I know I could look at the output of the preproces...

26
голосов
6ответов
59865 просмотров

Can the C preprocessor perform integer arithmetic?

As the questions says, is the C preprocessor able to do it? E.g.: #define PI 3.1416 #define OP PI/100 #define OP2 PI%100 Is there any way OP and/or OP2 get calculated in the preprocessing phase?

3
голосов
3ответов
2941 просмотров

How to include a newline in a C++ macro or how to use C++ templates to do the same?

I saw the following question: How to generate a newline in a cpp macro? Let me give a brief requirement of a need in newline in a C++ preprocessor. Am working on ARM Realview compiler 3.1 on a code which uses embedded assembly code with C++ code. #define DEFINE_FUNCTION(rtype, op, val) \ __...

41
голосов
5ответов
19987 просмотров

How do I temporarily disable a macro expansion in C/C++?

For some reason I need to temporarily disable some macros in a header file and the #undef MACRONAME will make the code compile but it will undef the existing macro. Is there a way of just disabling it? I should mention that you do not really know the values of the macros and that I'm looking f...

23
голосов
6ответов
13914 просмотров

What C preprocessor conditional should I use for OS X specific code?

What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux. I know there is __APPLE__ but I don't know if that is a current conditional for OS X 10.x.

3
голосов
4ответов
320 просмотров

Generating and saving code from Macro

I am analyzing a legacy code which heavily using macro, I am lost in understanding how macro are expanding in code. Could any one suggest me some tool or technique so that I can study actual code generated from macro expansion. Platform : Windows XP Language : C++ Compiler : VC6

3
голосов
4ответов
347 просмотров

What is the difference between these compiler directives?

What, if anything, is the difference between these directives? #ifdef FOO #if defined FOO #if defined(FOO) I'm using the CCS compiler, but I'm interested in other C compilers as well.

44
голосов
6ответов
28796 просмотров

Can you make custom operators in C++?

Is it possible to make a custom operator so you can do things like this? if ("Hello, world!" contains "Hello") ... Note: this is a separate question from "Is it a good idea to..." ;)

0
голосов
4ответов
3509 просмотров

Highlighting #defined value in VIM

I have XYZ highlighted in the header file where I have defined XYZ. However at the point of where it is used, XYZ is not highlighted. How would I fix this ? I have attached two screen shots (see TH_SYN in the code) to clarify my question- link text Any pointers are welcome.

55
голосов
6ответов
26602 просмотров

Does the C preprocessor strip comments or expand macros first?

Consider this (horrible, terrible, no good, very bad) code structure: #define foo(x) // commented out debugging code // Misformatted to not obscure the point if (a) foo(a); bar(a); I've seen two compilers' preprocessors generate different results on this code: if (a) bar(a); and if (a) ; ...

0
голосов
3ответов
146 просмотров

Function declarations and an unresolved external

I am looking after a huge old C program and converting it to C++ (which I'm new to). There are a great many complicated preprocessor hacks going on connected to the fact that the program must run on many different platforms in many different configurations. In one file (call it file1.c) I am ca...

2
голосов
4ответов
178 просмотров

Optional initializer, through preprocessor trickery?

I know that this will not work, but hopefully you can see what I'm trying to do #if ASSIGN_ALLOWED #define MAYBE_SKIP_REST_OF_LINE #else #define MAYBE_SKIP_REST_OF_LINE ; // #endif char str[80] MAYBE_SKIP_REST_OF_LINE = "Hello\n"; long array[3] MAYBE_SKIP_REST_OF_LINE = { 7,8,9 }; int...

-1
голосов
3ответов
716 просмотров

How to cast template class ptr to normal class ptr in C++

I have question regarding macros. How could I cast through macro a template class to normal class. In example: #define RUNTIME_CLASS(class_name) ((CRuntimeClass*)(&class##class_name)) template<typename T> A {}; if (RUNTIME_CLASS(A)); I know that this code wouldn't compile because i...

153
голосов
2ответов
71919 просмотров

How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?

I am trying to write a program where the names of some functions are dependent on the value of a certain macro variable with a macro like this: #define VARIABLE 3 #define NAME(fun) fun ## _ ## VARIABLE int NAME(some_function)(int a); Unfortunately, the macro NAME() turns that into int some_f...

39
голосов
8ответов
25579 просмотров

Stringifying template arguments

Is it possible in C++ to stringify template arguments? I tried this: #define STRINGIFY(x) #x template <typename T> struct Stringify { Stringify() { cout<<STRINGIFY(T)<<endl; } }; int main() { Stringify<int> s; } But what I get is a 'T', ...

6
голосов
1ответов
4886 просмотров

XCode Preprocessor Macro for Configuration?

I want my XCode Objective-C project to be able to detect which configuration it is being built with. How can I achieve this?

3
голосов
1ответов
3677 просмотров

Xcode/GCC predefined macro for target name?

I was wondering if there is an Xcode or GCC preprocessor symbol for the target name of the application. For example if I'm building an application called "MonkeyChicken", is there a preprocessor symbol such that printf( __TARGET_NAME__ ) outputs: MonkeyChicken

5
голосов
2ответов
2304 просмотров

A C preprocessor macro to pack bitfields into a byte?

I'm getting into micro-controller hacking and while I'm very comfortable with bitwise operators and talking right to the hardware, I'm finding the resulting code very verbose and boilerplate. The higher level programmer in me wants to find an effective but efficient way to clean it up. For inst...

0
голосов
12ответов
1859 просмотров

Printing the contents of a file using the #include directive (preprocessor)

Say I have a file, t.txt, that contains the following two lines: one two Now, I would like to write a program which will #include that file somehow and print its contents, nothing more. That is, I want the contents of that file to appear in my code as a static text, at compile time. Any i...

6
голосов
2ответов
3337 просмотров

Using #include to load OpenCL code

I've seen this done long ago with hlsl/glsl shader code -- using an #include on the source code file that pastes the code into a char* so that no file IO happens at runtime. If I were to represent it as pseudo-code, it would look a little like this: #define CLSourceToString(filename) " #include...

8
голосов
7ответов
1783 просмотров

How do I tell if a C integer variable is signed?

As an exercise, I'd like to write a macro which tells me if an integer variable is signed. This is what I have so far and I get the results I expect if I try this on a char variable with gcc -fsigned-char or -funsigned-char. #define ISVARSIGNED(V) (V = -1, (V < 0) ? 1 : 0) Is this porta...

5
голосов
8ответов
13319 просмотров

C++ #define preprocessor

I need to know that does the #define directive in C++ declares global label? By global I mean visible in every file? I'm using Visual Studio 2008, (guess if that matters)

18
голосов
2ответов
4731 просмотров

How to tame the Windows headers (useful defines)?

In one of the answers to this question jalf spoke about useful define NOMINMAX, that could prevent from unwanted defining min/max macros. Are there other useful defines that can help to control windows.h (or other Windows headers, for instance Microsoft C Runtime headers or STL implementation) be...

4
голосов
7ответов
4059 просмотров

Cancelling std::cout code lines using preprocessor

One can remove all calls to printf() using #define printf. What if I have a lot of debug prints like std::cout << x << endl; ? How can I quickly switch off cout << statements in a single file using preprocessor?

9
голосов
5ответов
8610 просмотров

How to print a pound / hash via C preprocessor?

I need help doing the following: a preprocessor macro label(x) shall output "#x", e.g., #define label(x) ... if I call label(aname), the output shall be "#aname" (w/o quotes) I know, that the following tries were errors. #define label(x) #x // leads to "x" #define label(x) \#x // is \"x"...