Solved

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

Posted on 2004-04-26
4
330 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now