Unexplained syntax error in Visual Studio - c++

#ifndef _CXS_H
#define _CXS_H
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#ifdef MATLAB_MEX_FILE
#include "mex.h"
#endif
#ifdef __cplusplus
#ifndef NCOMPLEX
#include <complex>
typedef std::complex<double> cs_complex_t ;
#endif
extern "C" {
#else
#ifndef NCOMPLEX
#include <complex.h>
#define cs_complex_t double _Complex
#endif
#endif
#define CS_VER 2 /* CXSparse Version */
#define CS_SUBVER 3
#define CS_SUBSUB 0
#define CS_DATE "Jun 1, 2012" /* CXSparse release date */
#define CS_COPYRIGHT "Copyright (c) Timothy A. Davis, 2006-2012"
#define CXSPARSE
#include "SuiteSparse_config.h"
#define cs_long_t SuiteSparse_long
#define cs_long_t_id SuiteSparse_long_id
#define cs_long_t_max SuiteSparse_long_max
........................
typedef struct cs_ci_sparse /* matrix in compressed-column or triplet form */
{
int nzmax ; /* maximum number of entries */
int m ; /* number of rows */
int n ; /* number of columns */
int *p ; /* column pointers (size n+1) or col indices (size nzmax) */
int *i ; /* row indices, size nzmax */
cs_complex_t *x ; /* numerical values, size nzmax */
int nz ; /* # of entries in triplet matrix, -1 for compressed-col */
} cs_ci ;
....................
#ifdef __cplusplus
}
#endif
#endif
I am getting a compilation error:
Error 1 error C2143: syntax error : missing ';' before '*'
for the line:
cs_complex_t *x ; /* numerical values, size nzmax */
Due to the size of the file some irrelevant parts are missing. Is there something that could explain this error given this code segment? This project is a simulator like Spice.

The code you've posted will lead to exactly this error if the macro NCOMPLEX is defined. Both definitions of cs_complex_t are enclosed in #ifndef NCOMPLEX. So if the macro NCOMPLEX is defined, cs_complex_t will not be defined and will thus be an unresolvable identifier when the compiler encounters it in the definition of cs_ci_sparse.
I'd say that's a logical bug in the file. If the definition of cs_complex_t depends on the absence of NCOMPLEX, all uses of cs_complex_t should depend on it too.

Related

redefining a macro not working as expected

I am in C++ defining blocks that go in a special area of memory. I want to define a block, then define the address of the next block in a variable that gets redefined for each block.
#include <iostream>
using namespace std;
#define BASE_ADDRESS 0X1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100)
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK
#define NEXT_FREE_BLOCK (TYPE_B + 200)
// ---end of RWW memory map---
int main()
{
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
return 0;
}
Example code that I've looked up looks like this. But when I try to compile this, I get "compilation failed due to the following errors":
main.cpp:12:16: error: ‘NEXT_FREE_BLOCK’ was not declared in this scope
#define TYPE_B NEXT_FREE_BLOCK
^
main.cpp:14:26: note: in expansion of macro ‘TYPE_B’
#define NEXT_FREE_BLOCK (TYPE_B + 200)
^~~~~~
main.cpp:20:34: note: in expansion of macro ‘NEXT_FREE_BLOCK’
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
^~~~~~~~~~~~~~~
If I comment out the 'type b' lines, it runs as expected. If I comment out the #undef line, it tells me I've redefined the macro. I would like to redefine the macro without getting an error or warning; is there a way to do that?
Lets take a look at this example:
#define MYMACRO 0 //MYMACRO = 0
#define ANOTHERMACRO MYMACRO //ANOTHERMACRO = MYMACRO = 0
int main() {
return ANOTHERMACRO;
}
All good right?
But if we do this:
#define MYRECURSIVEMACRO 0 //MYRECURSIVEMACRO = 0
#define MYMACRO MYRECURSIVEMACRO //MYMACRO = MYRECURSIVEMACRO
#undef MYRECURSIVEMACRO // MYRECURSIVEMACRO does not exist
#define MYRECURSIVEMACRO (MYMACRO+10) //What was MYMACRO again? It was MYRECURSIVEMACRO, but now we are setting MYRECURSIVEMACRO, which right now is being defined, to itself plus 10, and now I am 100% confused
#define ANOTHERMACRO MYRECURSIVEMACRO //Now what?
int main() {
return ANOTHERMACRO;
}
We get this:
1>C:\dev\Stack Overflow\Source.cpp(520,9): error C2065: 'MYMACRO': undeclared identifier
(At least for MSVC++)
Now if we look at this:
#define BASE_ADDRESS 0X1000 //BASE_ADDRESS = 0x1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS //TYPE_A = BASE_ADDRESS = 0x1000
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100) //NEXT_FREE_BLOCK = 0x1000 + 100
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK //TYPE_B = NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK //NEXT_FREE_BLOCK is gone
#define NEXT_FREE_BLOCK (TYPE_B + 200) //Now what? Same problem as before!
// ---end of RWW memory map---
This is why it isn't working.

Are pragma push/pop_macro directive stacks unique to each macro?

Microsoft provides the following example:
// pragma_directives_pop_macro.cpp
// compile with: /W1
#include <stdio.h>
#define X 1
#define Y 2
int main() {
printf("%d",X);
printf(" %d",Y);
#define Y 3 // C4005
#pragma push_macro("Y")
#pragma push_macro("X")
printf(" %d",X);
#define X 2 // C4005
printf(" %d",X);
#pragma pop_macro("X")
printf(" %d",X);
#pragma pop_macro("Y")
printf(" %d",Y);
}
which outputs: 1 2 1 2 1 3
Is there a separate stack for macro "Y" and macro "X", or do they use the same stack for all macros?
Each macro has its own stack. As the documentation you link to says, push_macro “Saves the value of the macro-name macro on the top of the stack for this macro,” and pop_macro “Sets the value of the macro-name macro to the value on the top of the stack for this macro.”
We can confirm this interpretation with code that pops X and Y not in last-in-first-out-order yet recovers the values originally pushed for X and Y respectively:
#include <stdio.h>
int main(void)
{
#define X "X0"
#define Y "Y0"
printf("X=%s.\n", X);
printf("Y=%s.\n", Y);
#pragma push_macro("X")
printf("Pushed X.\n");
#pragma push_macro("Y")
printf("Pushed Y.\n");
#undef X
#undef Y
#define X "X1"
#define Y "Y1"
printf("X=%s.\n", X);
printf("Y=%s.\n", Y);
#pragma pop_macro("X")
printf("Popped X.\n");
#pragma pop_macro("Y")
printf("Popped Y.\n");
printf("X=%s.\n", X);
printf("Y=%s.\n", Y);
}
which prints:
X=X0.
Y=Y0.
Pushed X.
Pushed Y.
X=X1.
Y=Y1.
Popped X.
Popped Y.
X=X0.
Y=Y0.

using defined constant (from Makefile) to form a part of a variable name in C

Is it possible to use a defined constant to form a part of a variable name in C? The constant is defined by the Makefile.(actually I want to pass it by make argument as shown below)
For example, I want to define a number NUM and declare a variable using it.
#define NUM 5
/* I want to declare as below
int var5
using defined constant `NUM` above
*/
I tried
int var ## NUM;
but the ## concatenation is only for preprocessing macro manipulation and doesn't work.
Actually the constant NUM is passed in from Makefile (by CFLAGS += -DNUM=5) in mycase.
How can I do this?
ADD1
According to Shachar Shemesh's answer below, I tried this.
=== test.c
#include <stdio.h>
#define compound_id(name1, name2) name1##name2
int compound_id(mynum, NUM);
main()
{
mynum5 = 5;
printf("NUM = %d\n", NUM);
printf("mynum5 = %d\n", mynum5);
== Makefile
ifeq ($(NUM),5)
CFLAGS+=-DNUM=$(NUM)
endif
== command
make test NUM=5
I get the following result :
ckim#stph45:/tmp] make test NUM=5
cc -DNUM=5 test.c -o test
test.c: In function 'main':
test.c:8: error: 'mynum5' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
make: *** [test] Error 1
What is wrong?
ADD 2
: I tried this too,(no Makefile, just run make test)
=== test.c
#include <stdio.h>
#define NUM 5
#define mynum(X) (int mynum##X;)
mynum(NUM)
main()
{
printf("NUM = %d\n", NUM);
mynum5 = 5;
printf("mynum5 = %d\n", mynum5);
}
and get this error :
ckim#stph45:/tmp] make test
cc test.c -o test
test.c:5: error: expected identifier or '(' before 'int'
make: *** [test] Error 1
You need to use a macro to use concatenation of the preprocessor. However, to ensure that your NUM is also preprocessed, you have to use two levels of macros, for example:
#define CONCAT_IMPL(LHS, RHS) LHS ## RHS
#define CONCAT(LHS, RHS) CONCAT_IMPL(LHS, RHS)
See Argument Prescan.
One option you can use is multilevel macro expansion. Try this:
#include <stdio.h>
/* This emulates the constant that comes from your makefile. */
#define NUM 5
/* This allows VAR2() to be called with the value of NUM (5)
instead of just the string "NUM". */
#define VAR(x) VAR2(x)
/* This actually concatenates 'var' and '5' to be 'var5'. */
#define VAR2(x) var##x
/* This allows you to stringify your variable name.
Again, we're using multilevel macro expansion so we get the
desired output string "var5" and not "VAR(NUM)". */
#define MACRO_STRINGIFY(x) STRINGIFY(x)
#define STRINGIFY(x) #x
int main(int argc, char **argv)
{
/* Declare your variable */
int VAR(NUM);
/* Assign a value to your variable */
VAR(NUM) = 7;
/* Print your variable name and its value */
printf(MACRO_STRINGIFY(VAR(NUM)) " = %d\n", VAR(NUM));
return 0;
}
Here's the output:
var5 = 7
And in case you're confused why that is the output, here's what main() looks like after preprocessing (i.e. all macro expansion is complete) but before compiling:
int main(int argc, char **argv)
{
int var5;
var5 = 7;
printf("var5" " = %d\n", var5);
return 0;
}
You can see exactly what your macros turn into by preprocessing your source file. For example, if you are using gcc and your source file is source.c you could preprocess it like this, with the results being stored in source.i:
# gcc -E source.c -o source.i
#define compound_id(name1, name2) name1##name2
int compound_id(var, NUM);

C++ Macro Arguments w/ Token Concatenation?

I have a bunch of labeled servos, each one has its own calibrated min, mid and max pulse-width value.
// repository of calibrated servo pulse width values:
#define SERVO_0x01_MIN 165
#define SERVO_0x01_MID 347
#define SERVO_0x01_MAX 550
#define SERVO_0x02_MIN 165
#define SERVO_0x02_MID 347
#define SERVO_0x02_MAX 550
...
To simplify maintenance of the code, swapping a servo should only require changing a single macro definition value.
// maps certain positions on robot to the servo that is installed there
#define JOINT_0 0x02
#define JOINT_1 0x05
#define JOINT_2 0x0A
...
// function-like macros to resolve values from mapping
#define GET_MIN(servo) SERVO_##servo##_MIN
#define GET_MID(servo) SERVO_##servo##_MID
#define GET_MAX(servo) SERVO_##servo##_MAX
The problem I'm having is that calling a function-like macro with an argument that itself is a macro does not resolve to its terminal value:
// main
int main(void) {
// this works
int max_0x01 = GET_MAX(0x01); // int max_0x01 = 550;
// this doesn't
int max_joint_0 = GET_MAX(JOINT_0); // int max_joint_0 = SERVO_JOINT_0_MAX;
}
What can I do to make GET_MAX(JOINT_0) turn into 550 ?
#define GET_MAX(servo) GET_MAX2(servo)
#define GET_MAX2(servo) SERVO_##servo##_MAX
The preprocessor will perform expansion (textual replacement) upon a variadic macro until it can expand no further. So passing in JOINT_0, such as GET_MAX(JOINT_0) will expand to
GET_MAX2(0x02)
This gets further expanded to
SERVO_0x02_MAX
And finally replaced with the #define value 550

Print fully evaluated result of #define during compilation using #pragma message()

I have a quick question regarding printing the evaluated values of #defines using #pragma message. I'm using msvc++ in Visual Studio 2008.
Below is a simplified example:
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define WIDTH 10
#define HEIGHT 10
#define AREA (WIDTH * HEIGHT)
#pragma message("Area is: " __STR1__(AREA))
Now when I compile I get the following output:
>Area is: (10 * 10)
This is not exactly what I want. Is there any way to print out the evaluation of a #define expression so that I get:
>Area is: 100
during compilation. Perhaps this is not possible. Eventually I want to be able to cause a compiler error if the evaluated value is too large. i.e.
#if(AREA > 1000)
#pragma message(__ERROR__)
#endif
Some of my #defines use sizeof() which I believe causes issues in itself when evaluating conditionals - but that's a problem for the future!
I looked at the following post How do I show the value of a #define at compile time in gcc which is fine as long as the #define is defined as a value, and not a concatenation of other #defines.
The preprocessor won't do math for you, it can only substitute tokens and expand macros in a textual way.
If you want to calculate that value during compilation you should go for constexpr (http://en.cppreference.com/w/cpp/language/constexpr, more precisely this will hint the compiler to calculate it at compile-time)
#include <iostream>
#define WIDTH 10
#define HEIGHT 10
template<int a, int b>
constexpr int getArea() {
static_assert(a*b < 1000, "Area is too big");
return a*b;
}
const int area = getArea<WIDTH, HEIGHT>();
int main(void) {
std::cout << area;
}
Example
static_assert will do the check for the area if it is too large.
The precompiler can do limited math in #if statements. This may be sufficient for your needs:
#define WIDTH 10
#define HEIGHT 10
#define AREA (WIDTH * HEIGHT)
#if AREA > 1000
#error Oh bad, oh bad, oh bad
#endif
For more complex mathematics I would second what Marco A. said but you don't need that in a template or anything.
You can just put it up with all your #defines, for example:
#define WIDTH 10
#define HEIGHT 10
#define AREA (WIDTH * HEIGHT)
#define __ERROR__ "Oh bad, oh bad, oh bad"
static_assert(AREA < 1000, __ERROR__);
Or even simpler: static_assert(WIDTH * HEIGHT < 1000, "Oh bad, oh bad, oh bad");