【问题标题】:Eclipse C++ add shared library to main projectEclipse C++ 将共享库添加到主项目
【发布时间】:2015-06-29 06:36:34
【问题描述】:

我第一次在 Ubuntu 14.02 上使用 Eclipse 3.8.1 和 2 个项目。我来自 c# 世界,所以可能是 Eclipse 错误或 C++ 概念错误。

testmonitor:一个示例 C++ 项目。代码:

#include <iostream>

using namespace std;

int main() {

     cout << "Test program" << endl;
     log_access::test();

     return 0;
}

log_access 是一个共享库:log_access.cpp

#include <iostream>

namespace log_access {

    void test()
    {
        std::cout << "It worked!!!" << std::endl;
    }

}

我正在尝试构建一个共享库并将其链接到主项目。我去了Project -&gt; Properties -&gt; Project References,点击了我要引用的项目(共享库)。

没用……

然后我去Project -&gt; Properties -&gt; C/C++ General -&gt; Paths and Symbols -&gt; References Tab,点击我要引用的项目(共享库)。

没用……

目前我收到以下错误:

Invoking: GCC C++ Compiler
g++ -std:c++0x -I"home/projects/dev/sample/workspace/log_access" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testproject.d" -MT"src/testmonitor.d" -o "src/testmonitor.o" "../src/testmonitor.cpp"
../src/testmonitor.cpp: In function 'int main()':
../src/testmonitor.cpp:34.3: error: 'log_access' has not been declared
   log_access:test();
   ^
make: *** [src/testmonitor.o] Error 1

13:56:39 Build Finished (took 1s.246ms)

Obs:log_access 编译良好...

我非常感谢一些帮助...

【问题讨论】:

    标签: c++ eclipse shared-libraries


    【解决方案1】:

    您需要在主文件中包含您对 log_access::test 的定义

    #include "log_access.h"
    

    假设您有一个名为 log_access 的头文件(您不应该包含 .cpp 文件;使用它们来实现头文件中声明的方法。请参阅 here 了解原因)。

    【讨论】:

    • 除了化妆品(我从我的 Linux VM 输入代码 - 没有复制/粘贴) - 已经在帖子中更正,它看到问题是缺少头文件......所以,看到对于 C++ 中的每一段代码,我都需要一个头文件,即使仅在函数/命名空间上也是如此......
    • @Mendez 不一定。作为一种过度简化,将声明放在头文件中实质上允许其他文件访问通过包含头文件声明的类、方法等。通常,您编写的大多数类/函数应该在头文件和源文件之间进行拆分,但在很多情况下这并不理想
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 2011-06-13
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多