1

I am using g++ 4.6.3. Here's the output of g++ -v.

g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-  languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 

Here's a sample of my code:

#include "Word.h"
#include <string>
using namespace std;

pthread_mutex_t Word::_lock = PTHREAD_MUTEX_INITIALIZER;

Word::Word():
_occurrences(1)
{
    memset(_buf, 0, 25);
}

Word::Word(char *str):
_occurrences(1)
{
    memset(_buf, 0, 25);
    if (str != NULL)
    {
        strncpy(_buf, str, strlen(str));
    }
}

g++ -c -ansi or g++ -c -std=c++98 or g++ -c -std=c++03, none of these options are able to build the code correctly. I get the following compilation errors:

mriganka@ubuntu:~/WordCount$ make
g++ -c -g -ansi Word.cpp -o Word.o
Word.cpp: In constructor ‘Word::Word()’:
Word.cpp:10:21: error: ‘memset’ was not declared in this scope
Word.cpp: In constructor ‘Word::Word(char*)’:
Word.cpp:16:21: error: ‘memset’ was not declared in this scope
Word.cpp:19:34: error: ‘strlen’ was not declared in this scope
Word.cpp:19:35: error: ‘strncpy’ was not declared in this scope
Word.cpp: In member function ‘void Word::operator=(const Word&)’:
Word.cpp:37:42: error: ‘strlen’ was not declared in this scope
Word.cpp:37:43: error: ‘strncpy’ was not declared in this scope
Word.cpp: In copy constructor ‘Word::Word(const Word&)’:
Word.cpp:44:21: error: ‘memset’ was not declared in this scope
Word.cpp:45:52: error: ‘strlen’ was not declared in this scope
Word.cpp:45:53: error: ‘strncpy’ was not declared in this scope

So basically g++ 4.6.3 on Ubuntu 12.04 is not able to recognize the standard c++ headers. And I am not finding a way out of this situation.

Second problem:

In order to make progress, I included < string.h> instead of < string>. But now I am facing linking errors with my message queue and pthread library functions.

Here's the error that I am getting:

mriganka@ubuntu:~/WordCount$ make
g++ -c -g -ansi Word.cpp -o Word.o
g++ -lrt -I/usr/lib/i386-linux-gnu Word.o HashMap.o main.o -o word_count
main.o: In function `main':
/home/mriganka/WordCount/main.cpp:75: undefined reference to `pthread_create'
/home/mriganka/WordCount/main.cpp:90: undefined reference to `mq_open'
/home/mriganka/WordCount/main.cpp:93: undefined reference to `mq_getattr'
/home/mriganka/WordCount/main.cpp:113: undefined reference to `mq_send'
/home/mriganka/WordCount/main.cpp:123: undefined reference to `pthread_join'
/home/mriganka/WordCount/main.cpp:129: undefined reference to `mq_close'
/home/mriganka/WordCount/main.cpp:130: undefined reference to `mq_unlink'
main.o: In function `count_words(void*)':
/home/mriganka/WordCount/main.cpp:151: undefined reference to `mq_open'
/home/mriganka/WordCount/main.cpp:154: undefined reference to `mq_getattr'
/home/mriganka/WordCount/main.cpp:162: undefined reference to `mq_timedreceive'
collect2: ld returned 1 exit status

Here's my makefile:

CC=g++ 
CFLAGS=-c -g -ansi 
LDFLAGS=-lrt 
INC=-I/usr/lib/i386-linux-gnu 
SOURCES=Word.cpp HashMap.cpp main.cpp 
OBJECTS=$(SOURCES:.cpp=.o) EXECUTABLE=word_count

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(INC) -pthread $(OBJECTS) -o $@

.cpp.o: $(CC) $(CFLAGS) $< -o $@

clean: rm -f *.o word_count

Please help me to resolve both the issues. I searched online relentlessly for any solution of these problems, but no one seems to have encountered these issues.

Mriganka
  • 81
  • 2
  • 8

1 Answers1

3

Those items are in <string.h> (or <cstring>), not <string>.

For your second problem you need to link in the appropriate libraries. For pthread add -pthread to the compiler command line. For the message queue library you'll need a -l option that names the library and possibly a -L option to let the tools know where it is.

J. Polfer
  • 12,251
  • 10
  • 54
  • 83
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I am not sure I understand your response quite. Which items do you mean? – Mriganka Jul 04 '12 at 04:07
  • Thanks. The include helped to fix the compile error. I had corrected the pthread error earlier by including -pthread in linker options. I tried the -L option now to resolve the mq errors, but I am still getting those errors. – Mriganka Jul 04 '12 at 04:17
  • 1
    @Mriganka: Try moving `$(LDFLAGS)` to the end of the rule i.e. `$(EXECUTABLE): $(OBJECTS) $(CC) $(INC) -pthread $(OBJECTS) -o $@ $(LDFLAGS)`, most likely the library is not linking due to lazy linking – another.anon.coward Jul 04 '12 at 04:40
  • Amazing! that fixed it. Thanks. I spent the whole day trying to fix it myself or find some solution online before posting this question myself. Thanks a lot. But can you please explain why it worked by putting the LDFLAGS in the end? – Mriganka Jul 04 '12 at 04:47
  • @Mriganka: http://stackoverflow.com/a/9417334/12711 explains why library order matters on the linker command line (for the gnu ld linker and some linkers - this rule does not apply to Microsoft's linker). So the -lrt option has to come after the object files that depend on the librt library. – Michael Burr Jul 04 '12 at 06:40