How to know where an identifier was defined before

Pages: 12
I am trying to compile a program which consists of multiple c and fortran source files. There is one C source which refuses to compile, because an identifier was defined somewhere else before it (I think)

The part of the code where compilation stops:
1
2
3
4
5
   void F77_GetAcc(int_f77 *handle, int_f77 *ilo, int_f77 *ihi,
                                 int_f77 *jlo, int_f77 *jhi, void *buff) {
      
     DDI_GetAcc(*handle,*ilo-1,*ihi-1,*jlo-1,*jhi-1,buff);
   }


The source file is called ddi_fortran.c

I am compiling with MSVC++ 2017, and I am getting the error

Error C2059: syntax error : 'constant'

With Intel C/C++ compiler, I am getting:

error: expected an identifier

Now after some experimenting, I have found that putting #undef F77_GetAcc immediately before that line makes the error go away. Now I want to know where exacly F77_GetAcc was defined before, and whether putting #undef is going to harm the program in any way. Is there any way to pinpoint, where the identifier was defined before? The compiler output in command line does not give much information.

(Even though I am compiling on Windows, I thought the topic is general so I posted here, if it's not suitable, please let me know and I will move it)
Last edited on
Search ddi_fortran.c for where F77_GetAcc is defined. If it's not in the .c file then it must be in one of the header files that it includes. As for undefining it, that should be okay as long as you don't need the previous definition in ddi_fortran.c. It won't affect other .c files (look up "translation unit" for why).

The Windows forum is for Windows specific questions (the windows OS or development systems), so your question does not belong there.
handle is a keyword in microsoft gibbiersh extensions. it may be that variable name.
Now I want to know where exacly F77_GetAcc was defined before
Since you're using Visual Studio, just put the cursor on the identifier and press F12. That'll take you to where the macro was defined.

handle is a keyword in microsoft gibbiersh extensions.
It's not. HANDLE is defined as a macro, but that's a different story.
@dutch

I searched for F77_GetAcc, it's only present in one header file: ddi_fortran.h

# define F77_GetAcc F77_Extern(DDI_GETACC)

However, I am not sure why only F77_GetAcc is giving an error, because the header file also defines many other identifiers like F77_Init which are present in the ddi_fortran.c file, and does not cause any error.

@jonnin, so should I use #undef handle ?

@helios, I am using the MSVC++ compiler from the command line, I am not using Visual Studio. Is there any way to do that from command line?
What's the definition of F77_Init in the ddi_fortran.h file and in ddi_fortran.c ?
What's the definition of F77_Extern? It sounds like F77_GetAcc is supposed to be the signature for a foreign function interface (FFI) and that once the macro is expanded it'll look something like
 
extern "C" /*...*/ DDI_GETACC(/*...*/)
thus it makes sense that the compiler would complain about unexpected tokens.
If I'm right there shouldn't be any harm in undefining the macro in your own code. I'm guessing further down the header there's a place where the macro is used like
 
F77_GetAcc;
and that bit has already been compiled by the time the compiler gets to your code. But I'm wondering why you chose that name specifically.

I am using the MSVC++ compiler from the command line, I am not using Visual Studio. Is there any way to do that from command line?
Obviously not.
Is there any way to do that from command line?


find /n "F77_GetAcc" *.c
I dunno, I personally would change 'handle' to another name if it complains about it. If its not complaining, leave it be.

get a copy of grep for windows if you want to do a lot of commandline stuff.
Last edited on
Here is ddi_fortran.c : https://pastebin.com/i9bwpiMv , here's ddi_fortran.h : https://pastebin.com/33WEPpY3

@seeplus See the pasted files. It's not just F77_Init, there are a whole lot of other functions defined before F77_Acc, and none of them cause problems.

@lastchance Ok, I did that, the "F77_GetAcc" is only present in those two files, nothing else.

@helios Yes, the C codes expose some functions that can be called by fortran. I didn't choose the name, this is not my code. It is from an open-source chemistry software called GAMESS. Their developers have said that the programs compiled with Intel parallel studio XE 2011 and MSVC++ 2010. Of course, those compilers aren't available anymore, so I am using the oldest I could get, which is Intel 2017 and MSVC++ 2017.

@jonnin So, then do I have to change the name handle everywhere in the code? It's part of a big software, so I am wary about changing the names.
Yes, but somebody has kindly re-directed your function away to another function called F77_Extern and you will have to search for that.
# define F77_GetAcc F77_Extern(DDI_GETACC)

Just comment one of them out.

What an incredibly complicated way to avoid doing direct MPI calls!
@lastchance The F77_Extern is defined in f77_extern.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   
#  if defined _UNDERSCORES
#    if _UNDERSCORES == 0
#       define F77_Extern(Funct) Funct
#    elif _UNDERSCORES == 1
#       define F77_Extern(Funct) Funct ## _
#    elif _UNDERSCORES == 2
#       define F77_Extern(Funct) Funct ## __
#    else
#       error "_UNDERSCORES not properly defined."
#    endif
#  else
#    error "_UNDERSCORES not defined."
#  endif 

(that's the whole file)

I still can't figure out why F77_Acc is the only function causing problems.
Last edited on
Use the /P switch to get the preprocessor output and see what the definition is expanding into. We're kind of flying blind like this.

https://docs.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=msvc-160
https://docs.microsoft.com/en-us/cpp/build/reference/fi-preprocess-output-file-name?view=msvc-160
Last edited on
@helios Ok, I have used the /P switch, and it gave me an *.i file. However, the file is huge (4 MB). When I search for F77_GetAcc in that file, it returns no results.

How do I find which part of the file I need?
When I search for F77_GetAcc in that file, it returns no results.
Obviously, since F77_GetAcc is the macro.
Search for the comment just before the function, or search "*handle,*ilo-1,*ihi-1,*jlo-1,*jhi-1,buff". Either way, it'll probably be way at the bottom.
Well, searching for it gave me this:

1
2
3
4
5
   void 6(int_f77 *handle, int_f77 *ilo, int_f77 *ihi,
                                 int_f77 *jlo, int_f77 *jhi, void *buff) {
      
     DDI_GetAcc(*handle,*ilo-1,*ihi-1,*jlo-1,*jhi-1,buff);
   }


Also, here is the full ddi_fortran.i file : https://easyupload.io/nocrp3
There's your answer, then. F77_GetAcc is expanding into 6. Post f77_extern.h. I want to see the definition of F77_Extern.
I already posted it one of the posts above.

Here it is:


1
2
3
4
5
6
7
8
9
10
11
12
13
#  if defined _UNDERSCORES
#    if _UNDERSCORES == 0
#       define F77_Extern(Funct) Funct
#    elif _UNDERSCORES == 1
#       define F77_Extern(Funct) Funct ## _
#    elif _UNDERSCORES == 2
#       define F77_Extern(Funct) Funct ## __
#    else
#       error "_UNDERSCORES not properly defined."
#    endif
#  else
#    error "_UNDERSCORES not defined."
#  endif  


That's the whole file (minus the comments).
What is DDI_GETACC defined as? Is it 6?
Last edited on
@Ganado I don't know. Where do I look for it? in ddi_fortran.i ?
Pages: 12