![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
"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
|
|||
|
|||
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:
It's more cleaner. Hope this helps! Quote:
|
|
#3
|
||||
|
||||
Re: "Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.Quote:
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:
mycpp.h CPP / C++ / C Code:
mycpp.cpp CPP / C++ / C Code:
Output: Code:
...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
|
||||
|
||||
Re: "Linker error: Undefined symbol".....Problem accesing C variable in a cpp file.Quote:
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:
mycpp.cpp CPP / C++ / C Code:
main.c CPP / C++ / C Code:
Output: Code:
...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 GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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