Home > Software engineering >  How to find package and link library without cmake in simple cpp file?
How to find package and link library without cmake in simple cpp file?

Time:02-18

file name: main.cpp

#include<iostream>
#include"boolector.h"

using namespace std;
int main()
{
Btor* btor=boolector_new();
cout<<"hello world";
boolector_delete(btor);
}

What if I don't want to make CMake project, just a C file and still wants to link library as in CMake?

I want equivalent to following (in CmakeLists.txt) in g .

find_package(Boolector)
target_link_library(project_name Boolector::boolector)

Documentation can be found at Text.

/usr/local/bin/boolector 
/usr/local/include/boolector
/usr/local/include/boolector/boolector.h
/usr/local/include/boolector/btortypes.h
/usr/local/lib/libboolector.a
/usr/local/lib/libboolector.so

Result of locate boolector

/usr/local/lib/cmake/Boolector
/usr/local/lib/cmake/Boolector/BoolectorConfig.cmake
/usr/local/lib/cmake/Boolector/BoolectorConfigVersion.cmake
/usr/local/lib/cmake/Boolector/BoolectorTargets-release.cmake
/usr/local/lib/cmake/Boolector/BoolectorTargets.cmake

Result of locate Boolector

I am using CentOS.

CodePudding user response:

You can use -l, -L and -I options of g like:

g   -L /usr/local/lib/ -lboolector -I /usr/local/include/boolector main.cpp -o main
  • -l option is for setting the name of the library to be linked
  • -L option is for setting the path where the library to be linked has to be searched
  • -I option is for setting the path where header files of the library are present

To run the executable, you need to make sure that the libraries are there in the LD_LIBRARY_PATH environment variable.

export LD_LIBRARY_PATH=$LD_LIBARY_PATH:/usr/local/lib
./main
  • Related