Boost countWords function

Hi,

It's code doesn't compile. I don't know why. Please, try it yourself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/regex.hpp>

int countWords(std::string str) {
    std::vector< std::string > result;
    static const boost::regex re("\\s+");
    boost::algorithm::split_regex(result, str, re);
    return result.size();
}

int main(int argc, char** argv) {

    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
cpp_regex_traits.hpp:369: undefined reference to 
`boost::re_detail::cpp_regex_traits_char_layer<char>::init()'

regex_raw_buffer.hpp:131: undefined reference to 
`boost::re_detail::raw_storage::resize(unsigned int)'

perl_matcher_non_recursive.hpp:107: undefined reference to 
`boost::re_detail::get_mem_block()'

...
 


Thank you!
Last edited on
Boost regex isn't an independent header file, you have to compile a cpp file for it too. You can either compile the cpp with your project (the annoying way) or you can properly compile boost and link to the regex library file (the correct way).
Thanks!

I added boost_regex-mgw46-mt-d-1_54.a to NetBeans.

But :(

"/usr/bin/make" -f nbproject/Makefile-Debug.mk
QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/d/Documents/NetBeansProjects/
C++/CppExamples/Examples_0006_Boost_CountWords'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/
MinGW-Windows/examples_0006_boost_countwords.exe
make[2]: Entering directory `/d/Documents/NetBeansProjects/
C++/CppExamples/Examples_0006_Boost_CountWords'
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/examples_0006_boost_countwords
build/Debug/MinGW-Windows/main.o -lcppunit -lboost_regex-mgw46-mt-d-1_54
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe:
cannot find -lboost_regex-mgw46-mt-d-1_54
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/
examples_0006_boost_countwords.exe] Error 1
make[2]: Leaving directory `/d/Documents/NetBeansProjects
/C++/CppExamples/Examples_0006_Boost_CountWords'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/d/Documents/NetBeansProjects/C++/
CppExamples/Examples_0006_Boost_CountWords'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 15s)


cannot find -lboost_regex-mgw46-mt-d-1_54
Last edited on
Last edited on
Does NetBeans know to look for lib files there?
See:

http://i.pixs.ru/storage/4/4/1/60png_2799435_8871441.png

Output

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/examples_0006_boost_countwords.exe
make[2]: Entering directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/examples_0006_boost_countwords build/Debug/MinGW-Windows/main.o -lboost_regex-mgw46-mt-d-1_54
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lboost_regex-mgw46-mt-d-1_54
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/examples_0006_boost_countwords.exe] Error 1
make[2]: Leaving directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 2s)



Please, help!
Last edited on
Why I see "-l" in "cannot find -lboost_regex-mgw46-mt-d-1_54"

What is "-l"?
Last edited on
It's the flag indicating it's a library. It might be getting included in the file name somehow...
Lib name is "libboost_regex-mgw46-mt-d-1_54.a"

But we see "-lboost_regex-mgw46-mt-d-1_54"

What have I do?
I included the files:

http://i.pixs.ru/storage/8/2/0/61png_3611001_8872820.png

I added the library:

http://i.pixs.ru/storage/7/4/0/62png_9186930_8872740.png

This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/regex.hpp>

int countWords(std::string str) {
    std::vector< std::string > result;
    static const boost::regex re("\\s+");
    boost::algorithm::split_regex(result, str, re);
    return result.size();
}

int main(int argc, char** argv) {

    return 0;
}


Output:


"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/examples_0006_boost_countwords.exe
make[2]: Entering directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
mkdir -p build/Debug/MinGW-Windows
rm -f build/Debug/MinGW-Windows/main.o.d
g++    -c -g -I/C/boost/include/boost-1_54 -I/C/cppUnit/include -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW-Windows
g++     -o dist/Debug/MinGW-Windows/examples_0006_boost_countwords build/Debug/MinGW-Windows/main.o -lboost_regex-mgw46-mt-d-1_54
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lboost_regex-mgw46-mt-d-1_54
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/examples_0006_boost_countwords.exe] Error 1
make[2]: Leaving directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/d/Documents/NetBeansProjects/C++/CppExamples/Examples_0006_Boost_CountWords'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 10s)
Last edited on
Notice how I asked earlier:
L B wrote:
Does NetBeans know to look for lib files there?


But I am glad you figured it out ;)
Topic archived. No new replies allowed.