Solved

C module does not see global variable defined in C++ module

Posted on 2004-04-26
4
334 Views
Last Modified: 2010-04-01
I have a project in DevStudio that contains two files, main.cpp and foo.c:
/* main.cpp */
int glbl = 5;

extern "C" void foo();

int main(int argc, char *argv[])
{
      foo();
      return(0);
}
/* end of main.cpp */

/* foo.c */
#include <stdio.h>

extern int glbl;

void foo()
{
      printf("foo: glbl = %d\n", glbl);

      return;
}
/* end of foo.c */

When building, there are no compilation errors, but the linker gives me:
Compiling...
main.cpp
Linking...
foo.obj : error LNK2001: unresolved external symbol _glbl
Debug/CPP_C_externs.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

It seems that the definition of the global variable glbl is invisible in foo.c. How can I fix this?

Thanks!
0
Comment
Question by:ilyaz
  • 2
4 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 10919852
Must be some compiler specific stuff

worked fine forme  in cygwin

I used the following makefile

main: main.cpp foo.o
        g++ -o main main.cpp foo.o

foo.o: foo.c
        gcc -c foo.c

clean:
        rm -f *.o main


and it compiles fine and runs fine too

Btw atleast in cygwin ( gcc etc ) you have to take care that foo.c is compiled using gcc and not g++
i.e foo should be compiled with a C compiler and not a C++ compiler
can it be a similar problem you faced with DeVStudio ?

/abhijit/


0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 10919931
Try

/* main.cpp */
extern "C" int glbl = 5;

extern "C" void foo();

0
 
LVL 11

Expert Comment

by:avizit
ID: 10920006
>>
 Try

/* main.cpp */
extern "C" int glbl = 5;

extern "C" void foo();
>>


umm in that case i guess we won't have any definition of glbl  

in fact cygwin gives the following warning

main.cpp:1: warning: `glbl' initialized and declared `extern'
 the program works fine though

/abhijit/


/abhijit/
0
 
LVL 2

Expert Comment

by:colmcc
ID: 10925431
I think the problem may be the '_' prefix.  I'm guessing that C compilation adds this prefix and C++ does not.  I think cygwin automatically looks for names with and without the prefix when trying to resolves externals.

If this is the problem, then there may be some compiler/linker options you can change to get round it.  Or a very ugly solution would be to do the following in main.cpp -

int _glbl = 5;
#define glbl _glbl

Or, a better soution may be :

extern "C" int glbl;  // to get the linkage right.
int glbl = 5;           // to actually define the variable.

Colin.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question