Frage

I've read almost all the "undefined reference" topics here, have spent 2 days in trying different linkage options, but with no results. I'm desperate!

I'm on Linux Red Hat platform with Eclipse 3.6.1 and GCC 4.4.7.

I'm porting a large project from Solaris studio (where it works) to Eclipse. I have C++ Static Library project (e.g. Common_Lib) which includes transformations.c + .h files - pure C code.

The transformations.h file is wrapped with

#ifndef TRANSFORMATIONS_H_
#define TRANSFORMATIONS_H_

#ifdef __cplusplus
extern "C" {
#endif

void foo(int *x);

#ifdef __cplusplus
}
#endif
#endif

/* transformations.c */
#include "transformations.h"

void foo(int *x)
{
   /* calculations on x*/
}

I have another C++ static lib project (e.g. Factory_Lib) with method Do_Transform() which calls to foo() from transformations.h. This project is built properly.

#ifndef FACTORY_H
#define FACTORY_H
#include "transformations.h"

class CFactory
{
   public:
   void Do_Transform(int *x);
};

// factory.cpp
#include "factory.h"

void CFactory::Do_Transform(int *x)
{
   foo(x);
}

And the last one - executable - C++ project (e.g. Worker), which calls to Factory_Lib's Do_Transform(), which calls to foo().

#include factory.h

int main
{
   CFactory fact = new CFactory();
   int x=0;

   fact.Do_Transform(&x);

   return 0;
}

Worker refuses to be built (linked), "undefined reference to foo()" appears.

If the call to foo() is done directly from main() of Worker, the problem doesn't appear!

In Worker project I've tried to reference the Common_Lib in Reference projects, in Settings->Linker references, as additional libraries -l in the linker without the Referenced projects, etc. I've tried many combinations, but without success.

What must I do to make it work?!?!? I've run out of ideas.

Thank you in advance!

War es hilfreich?

Lösung

You may get a luck by the following: -lFactory -lCommon , the order is important.

Andere Tipps

As you have the code of both Common_Lib and Factory_Lib , why dont you compile both using the common c++ compiler itself. I mean compile both libs as a C++ library (no code change needed other than remove extern "C" from transformation.h )

I dont understand why you need extern here? There is no C compiler involved here. Both the libs are C++.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top