I keep getting the undefined reference error when calling a c function from c++ code:

void proxy(char *str);

In my funcs.h file, I use the

#ifdef __cplusplus
extern "C" {
#endif

wrapper, and include funcs.h in my c++ code. I also declare the function as

extern "C" void proxy(char *str);

above my main function in the c++ file. But when I call the function, I get the undefined reference error. Any help?

Where is proxy() defined?

Where is proxy() defined?

proxy() is defined in funcs.c, declared in funcs.h and sym.h, the .h file for sym.cpp

Show how you are using the call, including variable definitions. It may be something as simple as passing a const char* argument to the proxy() function, which requires a non-const char* argument.

Function proxy(char *file) is declared in both sym.hpp and funcs.h.
In funcs.h, I have, (among other things,

/* function declarations */

extern "C" void proxy(char *file);

In sym.h, I have

#ifdef __cplusplus	
extern "C"
{
	int lookup(int ndx, char *sym);
	int lookup2(int ndx, char *sym, int type);
	void set_ival(int ndx, int sel, int value);
	void set_fval(int ndx, float value);
	void set_ptr(int ndx, int sel, void *ptr);
	char *fetch_name(int ndx);
	int fetch_ival(int ndx, int sel);
	float fetch_fval(int ndx);
	void *fetch_ptr(int ndx, int sel);
	void proxy(char *file);	
}
 
#endif

In my funcs.c file, I define the function as:

void proxy(char *file)
{
	FILE *f;
	
	f = fopen(argv[j], "r");
		
	if(!f)
	{
		yyerror("Error opening file.");
		return(1);
	}	

	yyrestart(f);
	yylineno = 1;
	yyparse();

	fclose(f);
	return;
}

and from within c++, in my main function, I call it as:

using namespace std;

#include <string>
#include <iostream>
#include "sym.h"
extern "C"{
#include "funcs.h"
}

extern "C" void proxy(char*);

int main(int argc, char **argv)
{

	int j;
	
	if(argc < 2)
		cout << "No input file given." << endl;
	
	proxy(char *argv);

	return(0);
}

Granted, I'm guilty of overkill on declarations, but this is the result of too much frustration.
I hope this provides enough information.

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.