赞
踩
除非明确说明,本文内容仅针对x86/x86_64的Linux开发环境,有朋友说baidu不到,开个贴记录一下(加粗字体是关键词):
用“-Wl,-Bstatic”指定链接静态库,使用“-Wl,-Bdynamic”指定链接共享库,使用示例:
-Wl,-Bstatic -lmysqlclient_r -lssl -lcrypto -Wl,-Bdynamic -lrt -Wl,-Bdynamic -pthread -Wl,-Bstatic -lgtest
("-Wl"表示是传递给链接器ld的参数,而不是编译器gcc/g++的参数。)
/usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':
/home/software/mysql-5.5.24/mysys/my_compress.c:122: undefined reference to `uncompress'
/usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':
/home/software/mysql-5.5.24/mysys/my_compress.c:71: undefined reference to `compress'
/usr/local/mysql/lib/mysql/libmysqlclient.a(charset.c.o): In function `get_charset_name':
/home/zhangsan/mysql-5.5.24/mysys/charset.c:533: undefined reference to `pthread_once'
/usr/local/thirdparty/curl/lib/libcurl.a(libcurl_la-timeval.o): In function `curlx_tvnow':
timeval.c:(.text+0xe9): undefined reference to `clock_gettime'
/usr/local/thirdparty/openssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
dso_dlfcn.c:(.text+0x4c): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x62): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x6c): undefined reference to `dlclose'
ld: attempted static link of dynamic object
如果是以-L加-l方式指定,则目录下必须有.a文件存在,否则会报-l的库文件找不到:ld: cannot find -lACE
relocation R_x86_64_32S against `vtable for CMyClass` can not be used when making a shared object
test.cpp:35: error: expected `)' before 'PRIu64'
../../src/common/libmooon.a(logger.o): In function `atomic_dec_and_test':
../../include/mooon/sys/atomic_gcc.h:103: undefined reference to `__sync_sub_and_fetch_4'
error: expected declaration before '}' token
error: expected `}' at end of input
relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
./test.o: In function `gettid()':
./test.h:17: multiple definition of `gettid()'
正确的用法是使用"inline"或"static inline"修饰一下:
inline _syscall0(pid_t, gettid)
或
static inline _syscall0(pid_t, gettid)
当然也可以这样:
在.h头文件中:extern "C" pid_t gettid(void);
在.cpp文件中:_syscall0(pid_t, gettid)
_syscall0是一个宏,定义一个函数的实现。
my.cpp:364: warning: 'int my_function(const cgicc::Cgicc&, const std::string&)' defined but not used
只需使用“attribute((unused))”修饰函数的声明即可:
static int __attribute__((unused)) my_function(const cgicc::Cgicc&, const std::string&);
checking for setsockopt in -lsocket... no
checking for BN_init in -lcrypto... no
configure: error: "Error: libcrypto required."
原因可能是因为编译安装openssl时指定了–prefix,比如–prefix=/usr/local/thirdparty/openssl,可这样解决:
不指定thrift的configure的--with-openssl=/usr/local/thirdparty/openssl,改为:
CPPFLAGS="-I/usr/local/thirdparty/openssl/include" LDFLAGS="-ldl -L/usr/local/thirdparty/openssl/lib"
替代它就OK了。
/usr/include/c++/4.1.2/tr1/type_traits:408: error: 'size_t' is not a member of 'db_proxy::std'
/usr/include/c++/4.1.2/tr1/type_traits:408: error: 'size_t' is not a member of 'db_proxy::std'
/usr/include/c++/4.1.2/tr1/mu_iterate.h:49: error: 'std::tr1' has not been declared
/usr/include/c++/4.1.2/tr1/mu_iterate.h:49: error: 'std::tr1' has not been declared
/usr/include/c++/4.1.2/tr1/bind_iterate.h:78: error: 'std::tr1' has not been declared
/usr/include/c++/4.1.2/tr1/bind_iterate.h:78: error: 'std::tr1' has not been declared
/usr/include/c++/4.1.2/tr1/bind_iterate.h:78: error: 'std::tr1' has not been declared
/tmp/test.proto: File does not reside within any path specified using --proto_path (or -I). You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
解决办法有两个:一是在.proto文件所在目录下执行protoc,二是为protoc指定参数–proto_path,参数值为.proto文件所在目录。
error: expected constructor, destructor, or type conversion before '->' token
undefined symbol: EVP_enc_null
/usr/local/mysql/lib/mysql/libmysqlclient.a(charset.c.o): In function `get_charset_name':
/home/software/mysql-5.5.24/mysys/charset.c:533: undefined reference to `pthread_once'
/usr/local/mysql/lib/mysql/libmysqlclient.a(charset.c.o): In function `get_charset_number':
时遇到如下错误:
configure: error: No crypto library found!
Try --with-libssl-prefix=PATH
or --with-libgcrypt-prefix=PATH
or --with-wincng on Windows
可以如下方法解决:
./configure --prefix=/usr/local/thirdparty/libssh2-1.6.0 --with-openssl CPPFLAGS="-I/usr/local/thirdparty/openssl-1.0.2a/include" LDFLAGS="-L/usr/local/thirdparty/openssl-1.0.2a/lib"
UserInfoInternal* user_info_internal = new UserInfoInternal;
delete user_info; // 这里应当是user_info_internal
example.cpp:563:16: error: too many decimal points in number
如定义成了:#define IP1 127.0.0.1,改成#define IP1 "127.0.0.1"后问题即解决。
g++: -E or -x required when input is from standard input
如:
CPPFLAGS=-pthread -
g++ -g -o $@ $^ $(CPPFLAGS) $(LDFLAGS)
makefile:180: Extraneous text after `else' directive
makefile:183: *** only one `else' per conditional. Stop.
解决办法:
找到makefile文件的第180行,将“else ifeq ($(BUILD_TYPE), debug)”,改成两行内嵌方式:
# BUILD_TYPE specific settings
ifeq ($(BUILD_TYPE), small)
CXXFLAGS = $(cxxflags_small)
else
ifeq ($(BUILD_TYPE), debug) # 不能和else同一行,否则Makefile语法错误,不支持else ifeq
CXXFLAGS = $(cxxflags_debug)
libname := $(libname_debug)
else
CXXFLAGS ?= $(cxxflags_default)
endif
endif
可以通过设置环境变量来设置BUILD_TYPE,如:
export BUILD_TYPE=debug
也可以通过环境变量来设置make install时的安装目录,如:
export prefix=/usr/local/libjson
相关小知识:
在Makefile文件中,prefix=/usr和prefix?=/usr,是有区别的,前者赋值不能通过环境变量覆盖,后者则可以使用环境变量的值覆盖。
另外,请将第271行删除:
cp -rv $(srcdir)/Dependencies/ $(include_path)/$(libname_hdr)/$(srcdir)
改成:
cp -rv _internal/Dependencies/ $(include_path)/$(libname_hdr)/$(srcdir)
还有第258行前插入如一条命令:
mkdir -p $(inst_path)
否则“cp -f ./$(lib_target) $(inst_path)”,lib将成库文件名。
checking for x86_64-unknown-linux-gnu-nm... /data/gcc-4.8.2_src/host-x86_64-unknown-linux-gnu/gcc/nm
checking for x86_64-unknown-linux-gnu-ranlib... ranlib
checking for x86_64-unknown-linux-gnu-strip... strip
checking whether ln -s works... yes
checking for x86_64-unknown-linux-gnu-gcc... /data/gcc-4.8.2_src/host-x86_64-unknown-linux-gnu/gcc/xgcc -B/data/gcc-4.8.2_src/host-x86_64-unknown-linux-gnu/gcc/ -B/data/gcc-4.8.2/x86_64-unknown-linux-gnu/bin/ -B/data/gcc-4.8.2/x86_64-unknown-linux-gnu/lib/ -isystem /data/gcc-4.8.2/x86_64-unknown-linux-gnu/include -isystem /data/gcc-4.8.2/x86_64-unknown-linux-gnu/sys-include
checking for suffix of object files... configure: error: in `/data/gcc-4.8.2_src/x86_64-unknown-linux-gnu/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[2]: *** [configure-stage1-target-libgcc] 错误 1
所以只需要如下操作下即可:
export LD_LIBRARY_PATH=/usr/local/mpc/lib:/usr/local/mpfr/lib:/usr/local/gmp/lib:$LD_LIBRARY_PATH
注:gcc-4.8.2依赖mpc、mpfr和gmp:
./configure --prefix=/usr/local/gcc-4.8.2 --with-mpc=/usr/local/mpc-1.0.3 --with-mpfr=/usr/local/mpfr-3.1.3 --with-gmp=/usr/local/gmp-6.0.0
而mpc又依赖于:mpfr和gmp:
./configure --prefix=/usr/local/mpc-1.0.3 --with-mpfr=/usr/local/mpfr-3.1.3 --with-gmp=/usr/local/gmp-6.0.0
fatal error: gnu/stubs-32.h: No such file or directory
这是因为在x86_64上,默认会编译出32位和64位两个版本。这样编译32位时,需要机器上有32位的libc头文件和库文件,但一些机器上可能没有,比如没有/lib目录,只有/lib64目录,这表示不支持32位的libc。为解决这个问题,可以禁止编译32位版本,在configure时带上参数–disable-multilib,或者安装32位版本的glibc。
按常理,这个错误要么是没有指定相应的库,要么是静态库间的顺序问题。
但经过检查,这两个原因,而是因为gcc和g++混用原因:
1. 库libhiredis.a和libhiredis.so是由gcc编译出来的
2. 而调用它的代码是由g++编译的,因此导致了此问题。
问题的解决办法有两个:
3. 修改sdscatlen所在的.h文件,将代码使用
#ifdef __cplusplus
extern "C" {
#endif
修饰起来,或者直接使用“extern C”修饰函数sdscatlen。
extern "C" {
#include "sds.h"
}
上面两个办法均可,当然也可以考虑改用g++编译redis,不过可能会遇到很多错误。
redis对外供外部直接使用的头文件hiredis.h已使用了extern “C” {,所以不存在问题,只有当跳过hiredis.h,去使用一些内部头文件时需要注意一下。
x.hpp:27: error: expected `}' before string constant
x.hpp:27: error: expected unqualified-id before string constant
这个错误,可能是存在和枚举等同名的字符串宏,比如存在下面的宏定义:
enum DBTYPE
{
UNDEFINE = 0,
MYSQL_DB = 1,
ORACLE_DB = 2
};
而另一.h文件中定义了宏:
#define MYSQL_DB "mysql"
test.cpp:201:6: 错误:‘bool foo(const string&, const string&, const string&, const string&, const string&, const HBaseRowValue&)’的原型不匹配类‘CTest’中的任何一个
bool CHBaseOper::foo(const std::string& tablename, const std::string& rowkey, const std::string& familyname, const std::string& columnname, const std::string& columnvalue, const HBaseRowValue& row_values)
^
In file included from test.cpp:8:0:
test.h:58:6: 错误:备选为:int foo(const string&, const string&, const string&, const string&, const string&, const HBaseRowValue&)
int foo(const std::string& tablename, const std::string& rowkey, const std::string& familyname, const std::string& columnname, const std::string& columnvalue, const HBaseRowValue& row_values);
该编译错误原因是:
CMessager:CMessager()
{
}
即少了个冒号:
CMessager::CMessager()
{
}
编译时如果遇到这个错误,表示遇到一个gcc的bug,最简单的办法是去掉编译参数中的-O3等优化项,然后再试可能就成功了,也可以考虑指定-fno-schedule-insns。
/usr/include/c++/4.8.2/bits/stl_list.h:131:15: 错误:‘bidirectional_iterator_tag’不是命名空间‘gongyi::std’中的一个类型名
typedef std::bidirectional_iterator_tag iterator_category;
^
/usr/include/c++/4.8.2/bits/stl_list.h: 在成员函数‘_Tp* gongyi::std::_List_iterator<_tp>::operator->() const’中:
/usr/include/c++/4.8.2/bits/stl_list.h:150:16: 错误:‘__addressof’不是‘gongyi::std’的成员
{ return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
比如:
namespace A { namespace B {
。。。。。。
// 下面少了一个“}”
} // namespace A { namespace B {
错误是因为函数名和变量名相同了,可以改成如下解决:
uint32_t crc32 = ::crc32(0L, (const unsigned char*)crc32_str.data(), crc32_str.size());
/data/X/mooon/tools/hbase_stress.cpp:78:117: error: invalid conversion from 'void (*)(uint8_t, const string&, uint16_t) {aka void (*)(unsigned char, const std::basic_string<char>&, short unsigned int)}' to 'mooon::sys::FunctionWith3Parameter<unsigned char, std::basic_string<char>, short unsigned int>::FunctionPtr {aka void (*)(unsigned char, std::basic_string<char>, short unsigned int)}' [-fpermissive]
stress_threads[i] = new mooon::sys::CThreadEngine(mooon::sys::bind(write_thread, i, hbase_ip, hbase_port)); ^
In file included from /data/X/mooon/tools/hbase_stress.cpp:24:0:
/data/X/mooon/tools/../include/mooon/sys/thread_engine.h:777:16: error: initializing argument 1 of 'mooon::sys::Functor mooon::sys::bind(typename mooon::sys::FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>::FunctionPtr, Parameter1Type, Parameter2Type, Parameter3Type) [with Parameter1Type = unsigned char; Parameter2Type = std::basic_string<char>; Parameter3Type = short unsigned int; typename mooon::sys::FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>::FunctionPtr = void (*)(unsigned char, std::basic_string<char>, short unsigned int)]' [-fpermissive]
inline Functor bind(typename FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>::FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
上面这个错误的意思是第一个参数的类型为
void (*)(unsigned char, std::basic_string<char>, short unsigned int)
但传入的类型为
void (*)(unsigned char, const std::basic_string<char>&, short unsigned int)
从上面的对比可以看出,要求函数的第二个参数为std::string类型,而不是const std::string&类型。
这个错误可能是因为头文件没有#ifndef,导致引入多次。
答:将创建的项目后缀.c改为.cpp。
代码的std::map的类型使用了std::string,但是头文件包含里却没有添加#include,从而导致这些错误。
简单的说就是产生错误的工程的同时使用了CRT库和MFC库,并且链接顺序不对。CRT库对new、delete和dllmain函数使用弱外部链接。MFC库也包含以上三个函数。这些函数要求先连接MFC库,然后再连接CRT库。
在“项目”-“属性”-“链接器”-“命令行”中,输入“/verbose:lib”编译参数,这样就可以在output窗口看到链接时搜索的库的先后顺序。
这里需要强制改变一下它们的连接顺序。选择“项目”-“属性”-“链接器”-“输入”中,“忽略特定的库”这一项输入“MSVCRTD.lib mfcs90d.lib”,然后在“附加依赖项”这一项输入“mfcs90d.lib MSVCRTD.lib ”即可。
程序运行过程中,弹出中断对话框,显示出错文字如上。
有时Debug模式下弹出,Release模式不弹错误框。
这里的原因是数组越界。也有可能类型越界,比如long long变量向long变量赋值。
一般是栈被破坏,代码可能有缓冲区溢出一类的问题。而有的是调用代码中的临时变量在函数结束时提示出错。
strcpy、memcpy、strncpy等拷贝区的大小不匹配,引起冲突或溢出造成的。
当使用memset/ZeroMemory初始化数据结构体或数组时,由于大小设置错误引起的。
可能是指针移动、指向错误。
可能是使用了itao一类的函数,造成目标区和初始定义的大小不一致了。
long long keyL = 0;
bufIO.fread(&keyL, sizeof(long long), 1, fp);
long key = keyL;
问题描述:
1>c:\program files\microsoft sdks\windows\v6.0a\include\oaidl.h(809) : error C2143: 语法错误 : 缺少“}”(在“常量”的前面)
问题分析:
Win32工程容易出现。
解决方法:
添加头文件:#include <atlbase.h>
问题描述:
编译时没有错误,启动时弹出系统错误框,提示内容如上。
解决方法:
项目右键“属性”-“链接器”-“清单文件”-“生成清单”-“是”。
在解决方案中找到此项目中的文件xlist.rc节点右键菜单,选择子菜单“属性”。在界面“资源”-“常规”-“附加包含目录”中,填入值“$(IntDir)”。
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
int sprintf_s(
char *buffer,
size_t sizeOfBuffer,
const char *format [,
argument] ...
);
也可能出现另一个错误输出:
0xFEFEFEFE 处有未经处理的异常(在 test.exe 中): 0xC0000005: 执行位置 0xFEFEFEFE 时发生访问冲突。
2、WTL工程属性——配置属性——C/C++——常规——附加包含目录 添加WTL的Include目录
3、编译时出现error RC1015: cannot open include file ‘atlres.h’
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。