简体   繁体   中英

Eclipse: C-code in C++ static lib, referenced by C++ lib results in undefined reference

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 (eg 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 (eg 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 (eg 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!

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++.

您可能会遇到以下问题: -lFactory -lCommon ,顺序很重要。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM