【问题标题】:Unable to build my c++ code with g++ 4.6.3无法使用 g++ 4.6.3 构建我的 c++ 代码
【发布时间】:2012-07-04 03:53:02
【问题描述】:

我正在使用 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));
    }
}

g++ -c -ansi 或 g++ -c -std=c++98 或 g++ -c -std=c++03,这些选项都不能正确构建代码。我收到以下编译错误:

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

所以基本上 Ubuntu 12.04 上的 g++ 4.6.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

这是我的生成文件:

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

请帮我解决这两个问题。我在网上不停地搜索这些问题的任何解决方案,但似乎没有人遇到过这些问题。

【问题讨论】:

    标签: c++ ubuntu linker g++


    【解决方案1】:

    这些项目在&lt;string.h&gt;(或&lt;cstring&gt;)中,而不是&lt;string&gt;

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

    【讨论】:

    • 我不确定我是否完全理解您的回复。您指的是哪些项目?
    • 谢谢。 包含帮助修复编译错误。我之前通过在链接器选项中包含 -pthread 更正了 pthread 错误。我现在尝试了 -L 选项来解决 mq 错误,但我仍然收到这些错误。
    • @Mriganka:尝试将$(LDFLAGS) 移动到规则的末尾,即$(EXECUTABLE): $(OBJECTS) $(CC) $(INC) -pthread $(OBJECTS) -o $@ $(LDFLAGS),很可能由于延迟链接,库没有链接
    • 太棒了!解决了它。谢谢。在自己发布这个问题之前,我花了一整天的时间试图自己修复它或在网上找到一些解决方案。非常感谢。但是您能否通过将 LDFLAGS 放在最后来解释为什么它会起作用?
    • @Mriganka:@Mriganka:stackoverflow.com/a/9417334/12711 解释了为什么库顺序在链接器命令行上很重要(对于 gnu ld 链接器和一些链接器 - 此规则不适用于 Microsoft 的链接器)。所以 -lrt 选项必须在依赖于 librt 库的目标文件之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多