Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法生成我的c++;用g+编码+;4.6.3_C++_Ubuntu_Linker_G++ - Fatal编程技术网

C++ 无法生成我的c++;用g+编码+;4.6.3

C++ 无法生成我的c++;用g+编码+;4.6.3,c++,ubuntu,linker,g++,C++,Ubuntu,Linker,G++,我正在使用g++4.6.3。这是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-

我正在使用g++4.6.3。这是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) 
以下是我的代码示例:

#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));
    }
}
基本上,Ubuntu 12.04上的G+4.4.3不能识别标准的C++头。我没有找到摆脱这种局面的办法

第二个问题:

为了取得进展,我加入了而不是。但是现在我面临着消息队列和pthread库函数的链接错误

下面是我得到的错误:

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
这是我的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
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\u count
全部:$(源)$(可执行)
$(可执行文件):$(对象)$(CC)$(LDFLAGS)$(INC)-pthread$(对象)-o$@
.cpp.o:$(CC)$(CFLAGS)$<-o$@
清理:rm-f*.o字数
请帮我解决这两个问题。我在网上不懈地搜索这些问题的任何解决方案,但似乎没有人遇到这些问题。

这些项目在
(或
)中,而不是

对于第二个问题,您需要在适当的库中链接。对于pthread,将
-pthread
添加到编译器命令行。对于消息队列库,您需要一个命名库的
-l
选项,还可能需要一个
-l
选项来让工具知道它在哪里。

这些项目位于
(或
)中,而不是


对于第二个问题,您需要在适当的库中链接。对于pthread,将
-pthread
添加到编译器命令行。对于消息队列库,您需要一个命名库的
-l
选项,可能还需要一个
-l
选项,让工具知道它在哪里。

我不确定是否完全理解您的回答。你的意思是什么?谢谢。include帮助修复了编译错误。我之前已经通过在链接器选项中包含-pthread纠正了pthread错误。我现在尝试使用-L选项来解决mq错误,但仍然会遇到这些错误。@Mriganka:尝试将
$(LDFLAGS)
移动到规则的末尾,即
$(可执行文件):$(对象)$(CC)$(INC)-pthread$(对象)-o$@$(LDFLAGS)
,很可能是由于延迟链接而导致库未链接!这就解决了问题。谢谢我花了一整天的时间试图自己解决这个问题,或者在网上找到一些解决方案,然后才亲自发布这个问题。谢谢。但是你能解释一下为什么它会在最后加上LDFLAGS吗?@Mriganka:解释了为什么库顺序在链接器命令行上很重要(对于gnu ld链接器和一些链接器-这条规则不适用于Microsoft的链接器)。所以-lrt选项必须在依赖于librt库的对象文件之后出现。我不确定我是否完全理解您的响应。你的意思是什么?谢谢。include帮助修复了编译错误。我之前已经通过在链接器选项中包含-pthread纠正了pthread错误。我现在尝试使用-L选项来解决mq错误,但仍然会遇到这些错误。@Mriganka:尝试将
$(LDFLAGS)
移动到规则的末尾,即
$(可执行文件):$(对象)$(CC)$(INC)-pthread$(对象)-o$@$(LDFLAGS)
,很可能是由于延迟链接而导致库未链接!这就解决了问题。谢谢我花了一整天的时间试图自己解决这个问题,或者在网上找到一些解决方案,然后才亲自发布这个问题。谢谢。但是你能解释一下为什么它会在最后加上LDFLAGS吗?@Mriganka:解释了为什么库顺序在链接器命令行上很重要(对于gnu ld链接器和一些链接器-这条规则不适用于Microsoft的链接器)。因此-lrt选项必须位于依赖于librt库的对象文件之后。
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