简体   繁体   中英

How to compile and link boost libraries within Eclipse on Oracle Linux

Having trouble figuring out how to compile and link a simple boost program that just includes some boost libraries.

The code is as follows

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

Note to the moderators: there are a lot of posts on SO asking similar questions. However, I didn't find one that completely solved my issue (the key insight I was missing was that I needed to install boost-devel ). Thus, I created this Q&A.

Here are the steps necessary to compile your program

  1. Install boost and boost-devel

    sudo yum install boost # installs the libraries for linking sudo yum install boost-devel # installs the header files for you to include for compilation

  2. The command you're trying to get Eclipse to produce is the following:

    g++ net-server.cpp -I /usr/include -L/usr/lib64/ -lboost_system

The -L/usr/lib64 is actually unnecessary, but I include it for the reader, since it allows the reader more easily understand the relationship between the -l inclusion and the -L/usr/lib64 that's by default included in Oracle Linux (probably RHEL too).

The -I /usr/include is also unnecessary, but it's included to make it clear to the reader where the boost/array.hpp etc. files are coming from for the compilation phase.

  1. To get Eclipse to produce that command

    • Go to project > properties
    • Choose the build you wish to configure (you'll probably want to do it for both the "Debug" and "Release" builds, but maybe you just have one)
    • Go to the "Cross G++ Linker" > Libraries.
    • In the Libraries section, click on the + icon and add the boost_system library.

Heads up that I chose the "cross gcc compilation" option instead of the Linux cross-compile option. This is actually from within Nvidia's version of Eclipse (distributed with CUDA as "Eclipse Nsight"), so the instructions for the non-Nvidia version of Eclipse may vary a bit.

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