Monday, February 21, 2022

[SOLVED] How to include a C++ library after I `make` it?

Issue

I followed the instructions to build arrow which is

git clone https://github.com/apache/arrow.git
cd arrow/cpp
mkdir release
cd release
cmake ..
make

so now I want to make use of the libraries I have built

main.cpp

#include "parquet/arrow/writer.h"

void main(int argc, char *argv[]) {
    printf("ok")
}

but it complains that

In file included from /home/xiaodai/git/arrow/cpp/src/parquet/arrow/writer.h:24:0,
                 from main.cpp:1:
/home/xiaodai/git/arrow/cpp/src/parquet/properties.h:30:10: fatal error: parquet/parquet_version.h: No such file or directory
 #include "parquet/parquet_version.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~

so how do I tell gcc to use the library I just built?


Solution

Step 1. Installation

git clone https://github.com/apache/arrow.git
cd arrow/cpp
mkdir release
cd release
cmake .. -DCMAKE_INSTALL_PREFIX=<install_path> -DARROW_PARQUET=ON
make
make install

Step 2. Compilation and linking

g++ main.cpp -I<install_path>/include -L<install_path>/lib -lparquet -larrow -o main

main.cpp should be fixed (void main, missing ;) before you try to compile it.

Once I compiled successful, I run ./main and I get ./main: error while loading shared libraries: libparquet.so.18: cannot open shared object file: No such file or directory which is weird because <install_path>/lib clearly has the file libparquet.so.18!

The problem is that the dynamic loader cannot find the dynamic library. It doesn't know that <install_path>/lib should also be examined, and needs your help:

  • add <install_path>/lib into LD_LIBRARY_PATH:
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<install_path>/lib
    
  • or hardcode library path into the executable (not a good idea in general) with rpath by adding -Wl,-rpath=<install_path>/lib to g++ options.


Answered By - Evg
Answer Checked By - Cary Denson (WPSolving Admin)