Solved

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

Posted on 2004-04-26
4
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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