Hi all,
i have a problem which is i want to call function in different .cpp file.i create .h file. in main function, i include xxx.h as header file.in xxx.h i list down all the functios inside the file with extern.than in xxx.cpp i declare variable like dis e.g extern int selection; but i have an error which is unresolved external 'function called' referenced from 'main function file path'.

anyone has an idea about that????

thanks a lot

Recommended Answers

All 10 Replies

Post your code and trim it down to the smallest parts that can be compiled. I'd guess that you're missing something, like a definition for the function or you're not linking the files correctly.

here is d code:
//main.cpp
#include "xxx.h";

char b_selection [100]="";
void main(){
ExecuteBCombine (b_selection); }

//xxx.cpp
extern char *b_setting;
void ExecuteBCombine (char *b_setting){
}

//xxx.h
void extern ExecuteBCombine (char *b_setting);

what else im missing???really need help from expert like u guys...thanks a lot

Member Avatar for r.stiltskin

See comments below:

//main.cpp
#include "xxx.h";

char b_selection [100]="";
void main(){
ExecuteBCombine (b_selection); }

//xxx.cpp
extern char *b_setting;  // what is this?  where used? where defined?
void ExecuteBCombine (char *b_setting){
}

//xxx.h
// why do you declare this as extern?  why not just
// void ExecuteBCombine (char *b_setting);
void extern ExecuteBCombine (char *b_setting);

See comments below:

//main.cpp
#include "xxx.h";

char b_selection [100]="";
void main(){
ExecuteBCombine (b_selection); }

//xxx.cpp
extern char *b_setting;  // what is this?  where used? where defined?
void ExecuteBCombine (char *b_setting){
}

//xxx.h
// why do you declare this as extern?  why not just
// void ExecuteBCombine (char *b_setting);
 Extern void ExecuteBCombine (char *b_setting);

bcos i want to use the variable in main function

Member Avatar for r.stiltskin
extern char *b_setting;
void ExecuteBCombine (char *b_setting){
}

bcos i want to use the variable in main function

In the above code, there are two completely distinct and unrelated variables both named b_setting. The first one declares a variable of type char* that is defined elsewhere -- in this case in main(). The second one is a function parameter that exists only within the function. It could be named anything at all and would work just the same as long as you use the same name throughout that function.

Based on what you have posted it appears that there is no reason for extern char *b_setting; in xxx.cpp.

Do you want to use BOTH a pointer named b_setting and an array named b_selection in the function ExecuteBCombine()? If so, maybe your code should look like this:

//main.cpp
#include "xxx.h"  // note no ";" after preprocessor directives

void main(){
    char *b_setting;
    char b_selection [100]="";
    //...
    // do something to b_setting????
    //...
    ExecuteBCombine (b_selection, b_setting); // whatever happens to foo, happens to b_selection
}
//xxx.h
void ExecuteBCombine (char *foo, char *bar); // name the parameters whatever you like
//xxx.cpp
void ExecuteBCombine (char *foo, char *bar){ // name the parameters whatever you like
// use bar
// do something to foo
}

If not, I think you need to post more code to show more clearly what you are trying to do.

//this is main.cpp file.

#include "xxx.h"
char b_selection [100]="";
int main(int argc, char* argv[])
{
//case a to c bla bla bla
case 'b':  strcpy (b_selection,argv[++i]);
                           if ((int)strlen(b_selection)==GetParameter())
                            {
                             printf ("Binary combinations => %s\n",b_selection);
                             ExecuteBinaryCombinations (b_selection);
                            }
                           else
                            {
                             printf ("Invalid binary selection\n");
                            }
                           break;
                default:   printf ("Invalid selection \n");
                           break;


//this is xxx.cpp file
extern char *b_setting;
void ExecuteBCombine(char *b_setting)
 {
   char *modify_setting =(char *) malloc(strlen(b_setting));
   modify_setting=RemoveBlank (b_setting);
   setting_order = SetIndexValue(modify_setting);

   // Obtain the t value based on b_setting
   int t_value = GetTValue (modify_setting);
}
//this is xxx.h file

extern void  ExecuteBCombine(char *b_setting);

after i run i got this error.
unresolved external 'ExecuteBCombine(char *)' referenced from c:main.obj

Member Avatar for r.stiltskin

In xxx.h and xxx.cpp you have ExecuteBCombine, in main you have ExecuteBinaryCombinations, and yet the error message refers to ExecuteBCombine referenced from main? Is there a discrepancy like this in the code you are compiling or only in the code you are posting?

If the problem is not related to a discrepancy in the function name, are you compiling this on the command line or in an IDE?

Edit: It occurs to me that you may be forgetting to compile xxx.cpp. What command are you using to compile this code?

By the way, why are you using a C++ compiler when you seem to be using only C library functions?

actually they r same.ExecuteBCombine=ExecuteBinaryCombination.i change the name when i post here to make it short.

i use both c n c++.actually i have long code.only that 1 cos d problem.i stuck here.i use c++ builder 6 to compile my code.

Member Avatar for r.stiltskin

Did you add xxx.cpp to the project? I see nothing in the code you posted that should cause your problem, but the code you are posting is just uncompilable snippets. You need to post some simple, COMPILABLE code that reproduces your error. For example, I made these 3 little files based on yours:

//main.cpp

#include <cstring>
#include "xxx.h"

char b_selection [100]="";

int main(int argc, char* argv[])
{
  int i = 0;
  strcpy (b_selection,argv[++i]);
  ExecuteBCombine(b_selection);
}
//xxx.cpp

#include <cstdio>
#include <cstring>
#include <cstdlib>

extern char *b_setting;

void ExecuteBCombine(char *b_setting)
{
  char *modify_setting =(char *) malloc(strlen(b_setting));
  strcpy( modify_setting, b_setting );
  printf( "%s\n", modify_setting );
}
// xxx.h

extern void ExecuteBCombine(char *b_setting);

They compile and run correctly if I compile them correctly with g++ main.cpp xxx.cpp , but if I compile just g++ main.cpp I get an error similar to yours:

/tmp/ccsvk4o5.o: In function `main':
main.cpp:(.text+0x3f): undefined reference to `ExecuteBCombine(char*)'
collect2: ld returned 1 exit status

What happens when you compile my code?

(PS: the line extern char *b_setting; in my xxx.cpp does not cause any error, but it is also completely USELESS and UNNECESSARY. I still don't see anything in your code to show why you use that declaration.)

Did you add xxx.cpp to the project? I see nothing in the code you posted that should cause your problem, but the code you are posting is just uncompilable snippets. You need to post some simple, COMPILABLE code that reproduces your error. For example, I made these 3 little files based on yours:

//main.cpp

#include <cstring>
#include "xxx.h"

char b_selection [100]="";

int main(int argc, char* argv[])
{
  int i = 0;
  strcpy (b_selection,argv[++i]);
  ExecuteBCombine(b_selection);
}
//xxx.cpp

#include <cstdio>
#include <cstring>
#include <cstdlib>

extern char *b_setting;

void ExecuteBCombine(char *b_setting)
{
  char *modify_setting =(char *) malloc(strlen(b_setting));
  strcpy( modify_setting, b_setting );
  printf( "%s\n", modify_setting );
}
// xxx.h

extern void ExecuteBCombine(char *b_setting);

They compile and run correctly if I compile them correctly with g++ main.cpp xxx.cpp , but if I compile just g++ main.cpp I get an error similar to yours:

/tmp/ccsvk4o5.o: In function `main':
main.cpp:(.text+0x3f): undefined reference to `ExecuteBCombine(char*)'
collect2: ld returned 1 exit status

What happens when you compile my code?

(PS: the line extern char *b_setting; in my xxx.cpp does not cause any error, but it is also completely USELESS and UNNECESSARY. I still don't see anything in your code to show why you use that declaration.)

its work!!!thanks a lot dude......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.