Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-26
4
Medium Priority
?
343 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 500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

783 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