简体   繁体   English

Eclipse:C ++静态库中的C代码,由C ++库引用会导致未定义的引用

[英]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. 我在这里阅读了几乎所有“未定义的参考”主题,花了2天的时间尝试不同的链接选项,但没有结果。 I'm desperate! 我很绝望!

I'm on Linux Red Hat platform with Eclipse 3.6.1 and GCC 4.4.7. 我在具有Eclipse 3.6.1和GCC 4.4.7的Linux Red Hat平台上。

I'm porting a large project from Solaris studio (where it works) to Eclipse. 我正在将一个大型项目从Solaris Studio(在其工作的地方)移植到Eclipse。 I have C++ Static Library project (eg Common_Lib) which includes transformations.c + .h files - pure C code. 我有C ++静态库项目(例如Common_Lib),其中包括Transformations.c + .h文件-纯C代码。

The transformations.h file is wrapped with Transformations.h文件包装为

#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. 我还有一个带有方法Do_Transform()的C ++静态库项目(例如Factory_Lib),该方法从Transformations.h调用foo()。 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(). 最后一个-可执行文件-C ++项目(例如Worker),它调用Factory_Lib的Do_Transform(),后者调用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. Worker拒绝构建(链接),出现“ 对foo()的未定义引用 ”。

If the call to foo() is done directly from main() of Worker, the problem doesn't appear! 如果直接从Worker的main()调用foo(), 则不会出现问题!

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. 在Worker项目中,我尝试在“ 参考”项目中的“设置”->“链接器”引用中引用 Common_Lib,作为链接器中没有Referenced”项目的附加库-l等。我尝试了许多组合,但没有成功。

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. 当您拥有Common_Lib和Factory_Lib的代码时,为什么不使用公共c ++编译器本身来编译它们。 I mean compile both libs as a C++ library (no code change needed other than remove extern "C" from transformation.h ) 我的意思是将两个库都编译为C ++库(除了从Transformation.h中删除extern“ C”之外,无需更改代码)

I dont understand why you need extern here? 我不明白为什么你需要在这里实习? There is no C compiler involved here. 这里没有C编译器。 Both the libs are C++. 这两个库都是C ++。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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