Reputation: 2004
I read many questions about this now but I can't seem to link boost libraries. This is the code I'm trying to run:
#include <iostream>
#include <ctime>
#include <vector>
#include <stdio.h>
#include <string>
#include <sstream>
#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
void getFilesInDir(const fs::path& root, const string& ext, vector<fs::path>& ret)
{
if(!fs::exists(root) || !fs::is_directory(root)) return;
fs::directory_iterator it(root);
fs::directory_iterator endit;
while(it != endit)
{
if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
++it;
}
}
Trying to build causes many errors like this:
make all
g++ -lm -g -Wall -std=c++11 -pthread -L/usr/lib/x86_64-linux-gnu/ -lboost_filesystem -lboost_system main.cpp -o main.out $(pkg-config opencv --cflags --libs)
/tmp/ccubp4VK.o: In function `getFilesInDir(boost::filesystem::path const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<boost::filesystem::path, std::allocator<boost::filesystem::path> >&)':
/home/nettef/workspace/project//main.cpp:26: undefined reference to `boost::filesystem::path::extension() const'
/home/nettef/workspace/project//main.cpp:26: undefined reference to `boost::filesystem::path::filename() const'
I triple checked and libboost_system.so
as well as libboost_filesystem
are present in /usr/lib/x86_64-linux-gnu/
.
This is the make target:
CXX = g++
CXXFLAGS = -lm -g -Wall -std=c++11 -pthread -L/usr/lib/x86_64-linux-gnu/ -lboost_filesystem -lboost_system
OPENCV_INCLUDES = $$(pkg-config opencv --cflags --libs)
TEST_LIBS = -lcppunit
CURRENT_DIR = $(shell pwd)
all:
$(CXX) $(CXXFLAGS) main.cpp -o main.out $(OPENCV_INCLUDES)
Upvotes: 1
Views: 302
Reputation: 136505
You specified linker inputs in wrong order. main.cpp
must precede the libraries it needs:
g++ -o main.out -Wall -std=c++11 -g main.cpp -lm -pthread -L/usr/lib/x86_64-linux-gnu/ -lboost_filesystem -lboost_system $(pkg-config opencv --cflags --libs)
And you probably don't need -L/usr/lib/x86_64-linux-gnu/
as it is in the standard linker search path, see the output of ld --verbose | grep SEARCH_DIR
.
I would change your makefile this way:
CXX := g++
CXXFLAGS := -pthread -g -Wall -Wextra -std=c++11
LDFLAGS := -pthread -g
LDLIBS := -lm -lboost_filesystem -lboost_system
CPPFLAGS :=
OPENCV_INCLUDES := $(shell pkg-config opencv --cflags)
OPENCV_LDLIBS := $(shell pkg-config opencv --libs)
CURRENT_DIR = $(shell pwd)
all: main.out
.PHONY : all
main.out : LDLIBS += ${OPENCV_LDLIBS} -lcppunit
main.out : main.o
$(CXX) -o $@ $(LDFLAGS) $^ ${LDLIBS}
main.o : CPPFLAGS += ${OPENCV_INCLUDES}
main.o : main.cpp
${CXX} -c -o $@ ${CPPFLAGS} ${CXXFLAGS} $<
Upvotes: 1