Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

715 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