GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Jan-2010, 21:23
AnandVKulkarni AnandVKulkarni is offline
New Member
 
Join Date: Jan 2010
Posts: 4
AnandVKulkarni is on a distinguished road

"Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.


I am in process of converting a c file to a cpp file. That file earlier(when it was c) referred to a global defined in some other c file(which still remains a c file and will not be changed to cpp). The global variable is declared as extern in a header file that is included in cpp file. I have added the following at the beginning and end of the header file:
//beginning of header file
#ifdef __cplusplus
extern "C" {
#endif

//end of h file
#ifdef __cplusplus
}
#endif

Still I am facing linker errorr that the symbol is undefined. Am I missing something else? Please help.
  #2  
Old 01-Feb-2010, 01:01
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 265
ahbi82 has a spectacular aura aboutahbi82 has a spectacular aura about

Re: "Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.


Actually i got a better idea, anyway extern global variable is not encouraged. Why don't u have a "accessor" for ur variable.'

CPP / C++ / C Code:
// Declare as static
static int MY_VAR = 0;

int* GetMyVar(void)
{
    return &MY_VAR;
}


It's more cleaner.
Hope this helps!


Quote:
Originally Posted by AnandVKulkarni
I am in process of converting a c file to a cpp file. That file earlier(when it was c) referred to a global defined in some other c file(which still remains a c file and will not be changed to cpp). The global variable is declared as extern in a header file that is included in cpp file. I have added the following at the beginning and end of the header file:
//beginning of header file
#ifdef __cplusplus
extern "C" {
#endif

//end of h file
#ifdef __cplusplus
}
#endif

Still I am facing linker errorr that the symbol is undefined. Am I missing something else? Please help.
  #3  
Old 01-Feb-2010, 06:32
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 350
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: "Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.


Quote:
Originally Posted by AnandVKulkarni
I am in process of converting a c file to a cpp file. That file earlier(when it was c) referred to a global defined in some other c file(which still remains a c file and will not be changed to cpp). The global variable is declared as extern in a header file that is included in cpp file. I have added the following at the beginning and end of the header file:
//beginning of header file
#ifdef __cplusplus
extern "C" {
#endif

//end of h file
#ifdef __cplusplus
}
#endif

Still I am facing linker errorr that the symbol is undefined. Am I missing something else? Please help.


You didn't show us the real code, but it appears that you're not externing the actual symbol. Here is a working example. You would do well in the future to write your own, enormously simplified version, get that version to work and then apply the lessons learned to your more complex body of code.


myfile.c
CPP / C++ / C Code:
int global = 0;


mycpp.h
CPP / C++ / C Code:
#ifndef mycpp_h_
#define mycpp_h_

extern int global;

#endif


mycpp.cpp
CPP / C++ / C Code:
#include <cstdio>

#include "mycpp.h"

int main()
{
    std::printf("global = %d\n", global);
    global = 42;
    std::printf("global = %d\n", global);

    return 0;
}


Output:

Code:
gcc -g -Wall -W -pedantic -c myfile.c bash-3.2$ g++ -g -Wall -W -pedantic -o mycpp mycpp.cpp myfile.o bash-3.2$ ./mycpp global = 0 global = 42

...obviously, we need to link against the object file that contains the symbol. The ifdef _cplusplus business is for when you want to make a C++ defined symbol available to C code, not the other way around. In our case, we need only to declare it as extern, whether in a header file or at the top of our .cpp implementation that uses it.


MxB
  #4  
Old 01-Feb-2010, 07:08
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 350
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: "Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.


Quote:
Originally Posted by ahbi82
Actually i got a better idea, anyway extern global variable is not encouraged. Why don't u have a "accessor" for ur variable.'

CPP / C++ / C Code:
// Declare as static
static int MY_VAR = 0;

int* GetMyVar(void)
{
    return &MY_VAR;
}


It's more cleaner.
Hope this helps!

AnandVKulkarni

Try not to pay any attention to ahbi82's code or statements about "more cleaner." Whatever noise you see above, it doesn't relate to your stated objectives and whatever noise you see above doesn't "hide" the data from anyone any more or less effectively than a "global" does. In fact, it could be considered LESS USEFUL than using a global, since in this case you'd have to use pointer semantics and somehow have to remember that it isn't dynamically allocated along with anything else that is going on in the code.

It does not help a C++ body of code nor does it help any C body of code. It doesn't prevent anyone from directly manipulating the static variable AND the naming convention used suggests that it is a CONSTANT, which is obviously not necessarily true.

If you're going to use a global, the cleanest and easiest way is to simply use one. There are many reasons for preferring not to use globals. There are other, valid cases, where globals are useful and appropriate. Preferring not to use globals is a good thing, particularly until one understands them better.

ahbi82 ...please do not take this the wrong way, but we're not sending SMS text messages here. Please have the dignity to type complete words and sentences so that you do not confuse others as to whatever value it may be that you bring to this discussion. The guidelines specifically address "web-speak." As respondents to help requests, we should adhere to the guidelines as a matter of example.

AnandVKulkarni

Whether intending to or not, ahbi82 kind of sort of demonstrates how you would want to use ifdef __cplusplus ...of course, without actually demonstrating it, in a situation where a symbol is defined in a C++ body of code, but is needed by a C body of code.

Here is an example using the starting point of my previous reply:

mycpp.h
CPP / C++ / C Code:
#ifndef mycpp_h_
#define mycpp_h_

extern int global;

#ifdef __cplusplus
extern "C" {
#endif

int add(int a, int b);

#ifdef __cplusplus
}
#endif

#endif


mycpp.cpp
CPP / C++ / C Code:
#include <cstdio>

#include "mycpp.h"

#ifdef __cplusplus
extern "C" {
#endif

int add(int a, int b)
{
    return a + b;
}

#ifdef __cplusplus
}
#endif

#if CPP_BUILD

int main()
{
    std::printf("global = %d\n", global);
    global = 42;
    std::printf("global = %d\n", global);

    return 0;
}

#endif



main.c
CPP / C++ / C Code:
#include <stdio.h>

#include "mycpp.h"

int main()
{
    for(int i = 0; i < 10; i++)
    {
	printf("%d + 1 = %d\n", i, add(i, 1));
    }
    return 0;
}


Output:

Code:
bash-3.2$ g++ -g -Wall -W -pedantic -c mycpp.cpp bash-3.2$ gcc -g -Wall -W -pedantic -std=c99 -o main main.c mycpp.o bash-3.2$ ./main 0 + 1 = 1 1 + 1 = 2 2 + 1 = 3 3 + 1 = 4 4 + 1 = 5 5 + 1 = 6 6 + 1 = 7 7 + 1 = 8 8 + 1 = 9 9 + 1 = 10

...we would have to modify the previous invocation of the compiler to pass the CPP_BUILD in for a C++ build, but this should be a basis from which easier understanding of using C and C++ together is taken.


MxB
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the data obtain in one C file to another C file? TommyC C Programming Language 12 15-Jan-2008 09:51
Problem inserting (appending) in the middle of text file rajeev nair C++ Forum 3 17-Apr-2007 00:55
RAW File System on a CD Problem SemperFi Computer Software Forum - Windows 13 03-Sep-2004 06:12
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Read in File 1 and look at the 3rd value of each row mak90thug C++ Forum 1 20-Mar-2004 22:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:03.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.