Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am building a user defined shell where the user can dynamically load new pluggins into the shell.

In some of my shared libraries I reference global variables using the keyword extern, such as the pluggin below

C++
extern char *prompt;

int setprompt(char *argv[]) {
	prompt = argv[1];
	return 0;
}


only, when I execute my shell program (which links this library) I get the error

./setprompt.so: undefined symbol: prompt


Why can't I assign something to the global variable?

Here's the code from the shell code which includes the definition of prompt

C++
char *builtin_functions[64] =  {
	"comment", "setprompt", "cd", "bgjobs", "fg", "loadpluggin", "culater"
};
char *prompt = "upsh";
void *pointers_to_functions[64];
char *background_jobs[64];
int ptr_to_func_index = 7;
int bg_index = 0;


int main()
{
Posted
Updated 16-Nov-15 18:51pm
v2

So you have an executable and a shared library. The library exports symbols to be used by the executable.

But you also want the library to access a symbol from the executable. This is called 'reverse dependency' and your executable must also export the symbol. This can be done by using the --export-dynamic linker option. See also section 3.4. Creating a Shared Library of the Program Library HOWTO[^].
 
Share this answer
 
It has nothing to do with "global". This is a linker error, not compilation. You simply did not define prompt anywhere. The linker looks up for all object files/libraries and cannot find where it is defined. Apparently, you did not bother to defined it anywhere. I hope you understand what is definition and declaration, the basics of C and C conception of separate compilation model.

—SA
 
Share this answer
 
Comments
Member 12144933 17-Nov-15 0:33am    
Oh sorry, prompt was also defined in the main program -- the one that does the linking. It's defined as `char *prompt = "upsh";`
Sergey Alexandrovich Kryukov 17-Nov-15 0:40am    
If it really was defined, you would not have this error. It could be not a part of the solution, not defined in proper context, something like that.
For example, if it is defined in the body of the function main it only exists on the stack frame of this function.
You need to use static linkage, say, define it outside of any functions and types, in the top-level context of a compilation unit.
—SA
Member 12144933 17-Nov-15 0:42am    
...it is defined outside lol
Sergey Alexandrovich Kryukov 17-Nov-15 0:44am    
If it was properly defined, you would not get this error message.
—SA
Member 12144933 17-Nov-15 0:51am    
please see above edit
Most likely, your problem is that you don't understand how C linkage works. You really need to learn it, because this is one of the basics; without it, any non-nonsense C or C++ programming is simply impossible. Please see my comment to Solution 1.

You can start here:
https://bytes.com/topic/c/answers/168304-static-linkage-extern-c[^],
http://en.cppreference.com/w/cpp/language/storage_duration[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900