将 Boost 库添加到 OS X Eclipse 中的 C++ 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4435067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:29:24  来源:igfitidea点击:

Adding Boost Library to a C++ project in OS X Eclipse

c++eclipsemacosboost

提问by Jacob Schoen

I am have been attempting to get a C++ project setup using boost file system library using eclipse. I followed thesedirections to install boost on my system. The directions where pretty much

我一直在尝试使用 eclipse 使用 boost 文件系统库来设置 C++ 项目。我按照这些说明在我的系统上安装了 boost。方向差不多

  1. download
  2. extract
  3. run bootstrap.sh
  4. run ./bjam architecture=combined
  1. 下载
  2. 提炼
  3. 运行 bootstrap.sh
  4. 运行 ./bjam architecture=combined

That seemed to go fine, no errors. I then fired up eclipse and created a new test project called test with a single file called test.cpp. The code in it is:

这似乎很顺利,没有错误。然后我启动了 eclipse 并创建了一个名为 test 的新测试项目,其中包含一个名为 test.cpp 的文件。里面的代码是:

#include <stdio.h>
#include <boost/filesystem.hpp>

int main() {
    boost::filesystem::path path("/Users/schoen"); // random pathname
    bool result = boost::filesystem::is_directory(path);
    printf("Path is a directory : %d\n", result);
    return 0;
}

This is just something simple to make sure it is all set up correctly. Of course I tried to compile at this point and it failed. Did some googling and found thissite. It said to add the boost library to the linker by going to project properties and adding "boost_filesystem". I tried this, and well it didn't work.

这只是确保所有设置正确的简单操作。当然,此时我尝试编译,但失败了。做了一些谷歌搜索并找到了这个网站。它说通过转到项目属性并添加“boost_filesystem”将boost库添加到链接器。我试过这个,但它没有用。

Can someone point me in the right direction or give me a hint to how to set up Boost in an Eclipse project?

有人能指出我正确的方向或给我一个关于如何在 Eclipse 项目中设置 Boost 的提示吗?

I am new to C++ and Eclipse, and most my experience is in Java with Netbeans. So I am pretty lost at the moment.

我是 C++ 和 Eclipse 的新手,我的大部分经验是 Java 和 Netbeans。所以我现在很迷茫。

UPDATE

更新

I just wanted to update on what I have tried based on the answers given.

我只是想根据给出的答案更新我尝试过的内容。

Based on Alex's suggestion I added boost_system and boost_filesystem to the linker list. I was still getting the same compiler errors.

根据 Alex 的建议,我将 boost_system 和 boost_filesystem 添加到链接器列表中。我仍然遇到相同的编译器错误。

Following the suggestion from rve I added the path to the boost libraries to the Library search path. When this did not work. I cleared out the linker list and tried it with just the library search path. This also did not work.

按照 rve 的建议,我将 boost 库的路径添加到了库搜索路径中。当这不起作用时。我清除了链接器列表并仅使用库搜索路径进行了尝试。这也不起作用。

I then cleared the Library search path. I then manually edited the command on the linker window to be 'g++ -L/Users/jacobschoen/Library/boost_1_45_0/stage/lib -lboost -lboost_filesystem'. This also did not work.

然后我清除了图书馆搜索路径。然后我在链接器窗口上手动将命令编辑为“g++ -L/Users/jacobschoen/Library/boost_1_45_0/stage/lib -lboost -lboost_filesystem”。这也不起作用。

In all of these I tried setting the path to boost to be '/Users/jacobschoen/Library/boost_1_45_0' and '/Users/jacobschoen/Library/boost_1_45_0/stage/lib'. Neither worked.

在所有这些中,我尝试将 boost 路径设置为“/Users/jacobschoen/Library/boost_1_45_0”和“/Users/jacobschoen/Library/boost_1_45_0/stage/lib”。都没有工作。

As requested the comiler error for the above code is:

根据要求,上述代码的编译器错误是:

**** Build of configuration Debug for project test ****

make all 
Building file: ../src/test.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.d" -o"src/test.o" "../src/test.cpp"
../src/test.cpp:10:32: warning: boost/filesystem.hpp: No such file or directory
../src/test.cpp: In function 'int main()':
../src/test.cpp:13: error: 'boost' has not been declared
../src/test.cpp:13: error: expected `;' before 'path'
../src/test.cpp:14: error: 'boost' has not been declared
../src/test.cpp:14: error: 'path' was not declared in this scope
make: *** [src/test.o] Error 1

If any one has any further suggestions I am still trying.

如果有人有任何进一步的建议,我仍在尝试。

Second UpdateOn a suggestion by rholmes I added an include library along with the linker list and library search path. So now the compile error is:

第二次更新根据 rholmes 的建议,我添加了一个包含库以及链接器列表和库搜索路径。所以现在编译错误是:

**** Build of configuration Debug for project test ****

make all 
Building target: test
Invoking: MacOS X C++ Linker
g++ -L/Users/jacobschoen/Library/boost_1_45_0 -o "test"  ./src/test.o   -lboost_system -lboost_filesystem
ld: library not found for -lboost_system
collect2: ld returned 1 exit status
make: *** [test] Error 1

Any ideas?

有任何想法吗?

采纳答案by rholmes

Not sure where you do this in Eclipse these days, but under the include paths for Eclipse should be the path to the main boost directory (/Users/jacobschoen/Library/boost_1_45_0?). The compiler line should have something like the following in it, I would think:

现在不确定您在 Eclipse 中的哪个位置执行此操作,但是在 Eclipse 的包含路径下应该是主 boost 目录的路径(/Users/jacobschoen/Library/boost_1_45_0?)。编译器行中应该包含以下内容,我认为:

Invoking: GCC C++ Compiler

调用:GCC C++ 编译器

g++ -I/Users/jacobschoen/Library/boost_1_45_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD (etc..)

g++ -I/Users/jacobschoen/Library/boost_1_45_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD (etc..)

Update: Looking at my system, the linker path on yours might be more appropriately:

更新:看看我的系统,你的链接器路径可能更合适:

-I/Users/jacobschoen/Library/boost_1_45_0/stage/lib

-I/Users/jacobschoen/Library/boost_1_45_0/stage/lib

Depending, of course, upon how you've installed and built boost -- this is with my most recent attempt with a full source build. Depending upon how you obtained boost, this may or may not be different. I recently redid the boost on my Mac for 64 bit and haven't had much time to try it yet....

当然,这取决于您安装和构建 boost 的方式——这是我最近对完整源代码构建的尝试。根据您获得提升的方式,这可能会或可能不会有所不同。我最近在 Mac 上为 64 位重新进行了增强,但还没有太多时间尝试....

回答by Jacob Schoen

Just wanted to be clear on what actually worked, since it was kinda pieced together from a few answers.

只是想弄清楚什么是真正有效的,因为它是从几个答案拼凑起来的。

  1. Download the boost files and extract them to where you want to put them.
  2. In your terminal navigate to the directory and run ./bootstrap.sh
  3. When that is done run ./bjam(this takes a while so go smoke and get a cup of coffee)
  4. Open up your eclipse Project and go to Project > Properties > C/C++ Build > Settings
  5. Click on MacOS X C++ Linker > Libraries. You should see a split window with the top being for 'Libraries (-l)'. In this section add both boost_systemand boost_filesystem. In the bottom section it should be for 'Library Search Path (-L)'. Here you want to put the path to the stage/lib directory inside where you extracted the boost download. It should look similar to below:alt text
  6. Click GCC C++ Compiler > Includes. This will be a single pane where it says 'Include Paths (-I)', well I think it is an I as he font is weird and could be a lower case l also. Anyway in that section add the path to where you put boost without the stage/lib part. It should look like below:alt text
  1. 下载 boost 文件并将它们解压缩到您想要放置它们的位置。
  2. 在您的终端中导航到目录并运行 ./bootstrap.sh
  3. 完成后运行./bjam(这需要一段时间,所以去抽烟喝杯咖啡)
  4. 打开您的 eclipse 项目并转到 Project > Properties > C/C++ Build > Settings
  5. 单击 MacOS X C++ 链接器 > 库。您应该会看到一个拆分窗口,顶部是“库 (-l)”。在本节中添加boost_systemboost_filesystem。在底部应该是“图书馆搜索路径(-L)”。在这里,您希望将 stage/lib 目录的路径放在您提取 boost 下载的位置。它应该类似于下图:替代文字
  6. 单击 GCC C++ 编译器 > 包含。这将是一个显示“包含路径(-I)”的单一窗格,我认为它是一个 I,因为他的字体很奇怪,也可能是小写的 l。无论如何,在该部分中将路径添加到您放置 boost 的位置,而没有 stage/lib 部分。它应该如下所示:替代文字

Everything should compile now with out a problem, and if you need to use any other boost libraries it should be just a matter of adding it to the linker section where boost_filesystem and boost_system are. Enjoy.

现在一切都应该编译没有问题,如果您需要使用任何其他 boost 库,只需将它添加到 boost_filesystem 和 boost_system 所在的链接器部分。享受。

回答by Alex F

Add boost_system to the linker list, together with boost_filesystem.

将 boost_system 与 boost_filesystem 一起添加到链接器列表中。

回答by Nav

I had recently uninstalled the boost rpm and installed Boost like how you did. I had no problems running Boost programs in Eclipse. I didn't add any extra parameters. Just installed boost and ran Boost programs. It works fine.

我最近卸载了 boost rpm 并像你一样安装了 Boost。我在 Eclipse 中运行 Boost 程序没有问题。我没有添加任何额外的参数。刚刚安装了 boost 并运行了 Boost 程序。它工作正常。

Tried your program in the vi editor. Commented out everything in main

在 vi 编辑器中尝试了您的程序。注释掉 main 中的所有内容

#include <cstdio>
#include <boost/filesystem.hpp>

int main() {
    /*boost::filesystem::path path("/Users/schoen"); // random pathname
    bool result = boost::filesystem::is_directory(path);
    printf("Path is a directory : %d\n", result);*/
    return 0;
}

and it still gave this error:

它仍然给出了这个错误:

/tmp/cc7TAIYS.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x29): undefined reference to `boost::system::get_system_category()'
test.cpp:(.text+0x35): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x41): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x4d): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x59): undefined reference to `boost::system::get_system_category()'
collect2: ld returned 1 exit status

I'm puzzled. Boost programs work on my system, but your program's header files itself are giving a problem. I doubt it's a problem with Eclipse. It has to be something else.

我很困惑。Boost 程序可以在我的系统上运行,但是您的程序的头文件本身存在问题。我怀疑这是 Eclipse 的问题。它必须是别的东西。

回答by Zak

I just ran into something very similar to this using eclipse and CDT... It turns out, using ubuntu and apt-get, libboost_system installs as libboost_system.1.40.0 in /usr/lib

我刚刚使用 eclipse 和 CDT 遇到了与此非常相似的事情......结果证明,使用 ubuntu 和 apt-get,libboost_system 在 /usr/lib 中安装为 libboost_system.1.40.0

If you try to add it via the library tab in Helios it will complain because it is looking for *.so and *.s0.1.40.0 clearly doesn't match that. However after looking closely at what the linker was trying to doo, I just typed the raw string "boost_system" into the include path adder. This resulted in the linker doing a " -lboost_system" which is a format the linker knows how to deal with in resolving version dependency... If you instead put in the full path to the .so file, the linker will just complain because it tries to do a " -l/usr/lib/libboost_system.so.1.40.0" .

如果您尝试通过 Helios 中的库选项卡添加它,它会抱怨,因为它正在寻找 *.so 和 *.s0.1.40.0 显然不匹配。然而,在仔细查看链接器试图做的事情之后,我只是将原始字符串“boost_system”输入到包含路径加法器中。这导致链接器执行“-lboost_system”,这是链接器在解析版本依赖性时知道如何处理的格式...如果您改为输入 .so 文件的完整路径,链接器只会抱怨,因为它尝试做一个 "-l/usr/lib/libboost_system.so.1.40.0" 。

So take my advice and just type in the simple " boost_system" after doing an apt-get install.. It will make it all very easy.

因此,请采纳我的建议,并在执行 apt-get 安装后输入简单的“boost_system”。这将使一切变得非常简单。