/usr/bin/ld: cannot find -llibboost - c++

Alright So right now I am attempting use the boost C++ libraries in Linux (Ubuntu 12.04) as I have previously used them in Windows. So using some example code from the Boost's site
testfile.cpp
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
Should very easily compile using this command
g++ -L/usr/local/lib -o "testfile" -llibboost_filesystem
My problem I am getting the linker error
/usr/bin/ld: cannot find -llibboost_filesystem
and cannot seem to figure out what I am missing. Please Help.

By convention, library names use the lib prefix on most Linux distributions. You should remove this prefix when instructing the linker which libraries to search for. Assuming the gnu ld linker, the documentation says
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to
link. This option may be used any number of times. If namespec is of the
form :filename, ld will search the library path for a file called filename,
otherwise it will search the library path for a file called libnamespec.a.
so you either want
g++ -L/usr/local/lib -o "testfile" -lboost_filesystem
or
g++ -L/usr/local/lib -o "testfile" -l :libboost_filesystem.so

Related

/usr/bin/ld: cannot find during linking g++

This question has already been here so many times. But I didn't find the answer.
I have this .cpp file
#include <clickhouse/client.h>
#include <iostream>
using namespace clickhouse;
int main(){
/// Initialize client connection.
Client client(ClientOptions().SetHost("localhost"));
client.Select("SELECT l.a, l.b from table", [] (const Block& block)
{
for (size_t i = 0; i < block.GetRowCount(); ++i) {
std::cout << block[0]->As<ColumnUInt64>()->At(i) << " "
<< block[1]->As<ColumnString>()->At(i) << "\n";
}
}
);
return 0;
}
and I have instantiated SO library, like written here.
after that i got the following structure of /usr/local/lib directory:
~/$ ls /usr/local/lib
>>libclickhouse-cpp-lib-static.a libclickhouse-cpp-lib.so
in next step I trying execute compilation with g++
~/$ g++ run.cpp -std=c++17 -o result -llibclickhouse-cpp-lib -L/usr/local/lib
>>/usr/bin/ld: cannot find -llibclickhouse-cpp-lib
>>collect2: error: ld returned 1 exit status
I don't know what hinders create links.
thank You for Your help!
ld's manual page describes the -l option as follows (irrelevant details omitted):
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of
files to link. [...] ld will search a directory for a library called
libnamespec.so
If you read this very carefully, you will reach the conclusion that -llibclickhouse-cpp-lib instructs ld to search for a library named liblibclickhouse-cpp-lib.so which, obviously, does not exist.
This should simply be -lclickhouse-cpp-lib.
in my case, the problem was solved when I change the Cmake version to 2.9
Try this:
g++ -std=c++11 -I./ -I./contrib -L./build/clickhouse/ -lclickhouse-cpp-lib-static -o demo demo.cpp ./build/clickhouse/libclickhouse-cpp-lib-static.a ./build/contrib/lz4/liblz4-lib.a ./build/contrib/cityhash/libcityhash-lib.a ./build/contrib/absl/libabsl-lib.a

Including external libraries in CodeRunner 2 app?

I've always used Xcode to compile OpenCV based code in c++. The procedure in Xcode was quite simple, I just had to mention the paths and add the necessary lib files to the project. Theres this app called CodeRunner 2 for macOS. Theres no proper documentation on how to include external libraries to compile code in this app. Is it possible to link OpenCV headers and compile them in CodeRunner ? If yes, could someone post the steps?
You can run OpenCV in CodeRunner by setting up a new language. Go to Preferences -> Languages, right-click C++, and select Duplicate. Name the new language "C++ OpenCV". On the right side of the preferences window, click Settings then the Edit Script button. Look for this line (or something similar):
xcrun clang++ -x c++ -lc++ -o "$out" "${files[#]}" "${#:1}"
Add the clang++ command line parameters for OpenCV after "$out". Here's my version:
xcrun clang++ -x c++ -lc++ -o "$out" -I/usr/local/opt/opencv3/include -L/usr/local/opt/opencv3/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_videoio -lopencv_calib3d "${files[#]}" "${#:1}"
Modify the -I and -L parameters to match your OpenCV install path. On this machine I used Homebrew to install OpenCV so it was installed in /usr/local/opt. On other machines I've compiled from source so OpenCV is installed in /usr/local/lib.
Modify the -l parameters to include the libraries you typically use.
After saving the compile script, go back to Preferences -> Languages and select the Templates button. You can set up a template for OpenCV programs. Here's mine:
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
int main(int argc, char *argv[]) {
cv::Mat image;
// read an image
if (argc < 2)
image = cv::imread("img.jpg");
else
image = cv::imread(argv[1]);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
// create image window named "asdfasdf"
cv::namedWindow("asdfasdf");
// show the image on window
cv::imshow("asdfasdf", image);
// wait for key
cv::waitKey(0);
return 0;
}
The previous reply by SSteve is great and also helps me sort out linking Boost library in CodeRunner.
Because the solution in the previous reply is specific to OpenCV library, a carelessly adding to the clang++ command line for external libraries in general might just generate massive building errors, which was the case when I tried to link Boost library.
Here, I want to clarify the unclear bit in SSteve's reply so everyone knows how and where to modify the command line before compiling their code with external library in Mac OS system.
I will use my case to explain, but in some point I will inform you of the tricky bits in CodeRunner setting or general command line typing.
I use macport to install the Boost library by
sudo port install boost
header file is located at /opt/local/include
library is located at /opt/local/lib/
If you cannot find the specific sub-library in Boost, open your terminal and type
cd /opt/local/lib/
find . -iname "*boost*"
and you should see all sub-libraries of Boost ( static library ends with .a and dynamic library ends with .dylib ) as below.
Before you start to modify the original command line ( supporting c++ 14 version ) such as
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
you need to know the directory of header file is after -I and the directory of Boost library is after -L, like
-I /opt/local/include/
-L /opt/local/lib/
In order to use a compiled static or dynamic sub-library in Boost ( see figure above ), you have to include it specifically after -L /opt/local/lib/. However, simply copying the library name without file extension either .a or .dylib would never let CodeRunner find the library you expect to run !!!
The detail is explained here and I just quota the important bit below
clang -dynamiclib -o libtest.dylib file1.o file2.o -L/some/library/path -lname_of_library_without_lib_prefix
To run such an example code in Boost Quickstart Document
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
the way to include <boost/regex.hpp> now is by
xcrun clang++ -x c++ -std=c++14 -stdlib=libc++ -lc++ -o "$out" -I /opt/local/include/ -L /opt/local/lib -lboost_regex-mt "${files[#]}" "${#:1}" ${CR_DEBUGGING:+-g}
By using this command line, you should be able to compile the example code with Boost library.
Just remember to replace the prefix -lib with -l and exclude the file extension in the command line.
At last, there are some alternative solution to include the external library by using Xcode, which is in here

How to use CityHash128 in c++ code?

I am trying to use google's cityhash hashing function. I am unable to link it to my c++ code. I have installed cityHash and it has generated libcityhash.la, etc files in my /usr/local/lib.
I am setting LD_LIB_LIBRARY=/usr/local/lib, but it doesn't seem to link to these files.
CODE:
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
std::ifstream file("dev/urandom");
char buff[4096];
file.read(buff, 4096);
const uint128 hashed = CityHash128(buff,4096);
file.close();
}
Compiling:
g++ -o city cityHash.cpp
Error:
/tmp/cctSoHTX.o: In function main:
cityHash.cpp:(.text+0x73): undefined reference to `CityHash128(char const*, unsigned long)'
collect2: error: ld returned 1 exit status
I include "city.h" and trying to compile it as follows:
g++ -I /usr/local/include/ -L/usr/local/lib -llibcityhash.a cityHash.cpp -o city
But i m still getting :undefined reference to `CityHash128(char const*, unsigned long)' –
Ok, it's the good old "order makes a difference". Instead of:
g++ -I /usr/local/include/ -L/usr/local/lib /usr/local/lib/libcityhash.a cityHash.cpp -o city
you should do:
g++ -I /usr/local/include/ -L/usr/local/lib cityHash.cpp -o city -lcityhash
(libraries and object files are processed in the order of appearance in the command line, and since none of the code so far has used anything from the library when you list it, nothing gets include from that library - then when you get to the actual code that does use it, you don't give the linker the library after it, so it can't find the symbol - note that this is dependant on the behaviour of the linker, so the same rules may not apply in for example a MS Visual Studio compiler/linker setup)

g++ can't find headers but I did include them

I am starting on c++ and already going wrong ...
I am trying to compile a small test of levelDB :
#include <assert.h>
#include "leveldb/db.h"
using namespace std;
int main() {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
return 1;
}
Here is the g++ command :
g++ -I include/ testLevelDB.cpp
Output:
/tmp/ccuBnfE7.o: In function `main':
testLevelDB.cpp:(.text+0x14): undefined reference to `leveldb::Options::Options()'
testLevelDB.cpp:(.text+0x57): undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)'
The include folder is the one with the levelDB headers.
You need to tell the linker to link to the leveldb library such as
g++ -I include/ testLevelDB.cpp -lleveldb
But this won't work if the library is not in /usr/lib or /usr/local/lib for that case assuming the libleveldb.so exists in some path called $LEVELDB_PATH you need to do
g++ -I include -L $LEVELDB_PATH testLevelDB.cpp -lleveldb
-L is much like -I but it tells the linker where to looks for libraries.
Also since you seem to be new to gcc world, please have a look at this gcc intro document.
It is a linkage error. Not related to the headers. Did you link with this lib (-l..) ?

xerces-c 2.8 : error while loading shared libraries

I'm trying to compile a program running on an HP UX server on a Red Hat Linux.
It uses xerces-c library to parse xml files. Compilation is ok, but when i try to run it, I get the following message
./a.out: error while loading shared
libraries: libxerces-c.so.28: cannot
open shared object file: No such file
or directory
I wrote a very simple program to try and understand whats going on:
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
int main(int argc, char* argv[])
{
return 0;
}
And compiled it like this:
g++ test.cpp
-L./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Surprisingly the file is actually there:
lib]$ ls
libxerces-c.a libxerces-c.so.28 libxerces-depdom.a libxerces-depdom.so.28
libxerces-c.so libxerces-c.so.28.0 libxerces-depdom.so libxerces-depdom.so.28.0
Any thoughts ? I feel i'm missing something, but don't know what.
Thanks in advance.
run ldd a.out and see if the linker can resolve the right .so file
export LD_LIBRARY_PATH to include the current folder (in the same manner as the PATH variable) and check ldd again
the good way to do what you want is the following one:
g++ test.cpp -Xlinker -R ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
or
g++ test.cpp -Wl,-rpath ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Xlinker or Wl options allow you to use specific linking options, you do not need to modifiy
LD_LIBRARY_PATH
You need to tell the runtime c library where to find the various symbols that arent compiled statically in your code and arent in the usualy /lib and /usr/lib locations.
You do this by adding the path to your shared library to LD_LIBRARY_PATH. In this case, this will be what you have been putting for the -L argument to the compiler.