随笔分类 -  C/C++

printf 的 %n
摘要:一出手,就写错误的代码。本想将整数转换成字符串,然后这样写:char buf[64];int num = 65;sprintf(buf, "%n", num);实际上应该是这样:sprintf(buf, "%d", num);查了一下,%n 是将当前 printf 已打印的字符数写入一个 int 指针。好久没用 c 了,慎之,慎之。 阅读全文

posted @ 2012-12-07 15:00 究生 阅读(1129) 评论(0) 推荐(0)

[转]cc gcc g++ 联系、区别
摘要:gcc is 'Gnu CompilerCollection'. If you pass it a C++ file, it will invoke the C++ compiler ('g++') behind the scenes.gcc is essentially the frontend for several compilers and the linker too.Edit:As several people pointed out, this doesn't mean that 'gcc' and 'g++' 阅读全文

posted @ 2012-11-27 10:56 究生 阅读(1549) 评论(0) 推荐(0)

函数声明 vs 变量定义
摘要:看一段代码:class Foo{ public: Foo() {}; Foo(int a) {}; void bar() {};};int main(){ // this works... Foo foo1(1); foo1.bar(); // this does not... Foo foo2(); foo2.bar(); return 0;}test.cpp:21: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’报错原因是,编译器把 Foo f... 阅读全文

posted @ 2012-10-10 10:29 究生 阅读(530) 评论(0) 推荐(0)

Makefile
摘要:1 # ---------------------------------------------------------------- 2 # This is a makefile which automatically updates itself, 3 # using "$(CC) -MM -E" in linux. 4 # linliu 2011-04-25 5 # ---------------------------------------------------------------- 6 # $* 不包括扩展名的目标文件名称 7 # $+ 所有依赖文件,以 阅读全文

posted @ 2012-09-14 10:35 究生 阅读(211) 评论(0) 推荐(0)

一个C语言宏展开问题
摘要:一个令人比较迷惑的问题,学C语言好多年,今天终于搞明白,记之。1 #define cat(x,y) x ## y2 #define xcat(x,y) cat(x,y)3 cat(cat(1,2),3) //为什么不是 123?4 xcat(xcat(1,2),3) //结果为什么是 123?要解答这个问题,首先看一下预处理过程的几个步骤:字符集转换(如三联字符)断行连接 /注释处理, /* comment */,被替换成空格执行预处理命令,如 #include、#define、#pragma、#error等转义字符替换相邻字符串拼接将预处理记号替换为词法记号在这里主要关注第4步,即如何展开. 阅读全文

posted @ 2011-03-22 11:31 究生 阅读(3298) 评论(0) 推荐(0)

小议size_t和size_type
摘要:偶尔在代码中中看到string::size_type,以前只用过size_t,很奇怪二者之间的关系。首先在c语言中,已经有size_t类型了,该类型是sizeof()操作符(注意sizeof()不是函数)的返回值类型,编译器在实现的时候通常size_t类型设置为unsigned int型。而C++中,string类型和许多其他库类型都定义了一些配套类型(companion type)。通过这些配套类型,库类型的使用就能与机器无关,size_type就是这些配套类型中的一种。string.find()函数的返回值就是size_type类型,注意下面的程序:1 string::size_type 阅读全文

posted @ 2010-11-26 17:52 究生 阅读(297) 评论(0) 推荐(0)

gcc: multiple definition of
摘要:/home/tace/openav/source/SeamlessMessage/CPaoFlt.o: In function `CPaoFlt::get_m_strPrmair() const':CPaoFlt.cpp:(.text+0x0): multiple definition of `CPaoFlt::get_m_strPrmair() const'/home/tace/openav/source/SeamlessMessage/CPaoFlt.o:CPaoFlt.cpp:(.text+0x0): first defined heregcc在编译过程中报函数重复定义( 阅读全文

posted @ 2010-11-17 10:16 究生 阅读(659) 评论(0) 推荐(0)

gcc -l参数和-L参数
摘要:l参数就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so去掉就是库名了。当我们自已要用到一个第三方提供的库名字libtest.so,那么我们只要把libtest.so拷贝到/usr/lib里,编译时加上-ltest参数,我们就能用上libtest.so库了(当然要用libtest.so库里的函数,我们还需要与libtest.so配套的头文件)。放在/lib和/usr/lib里的库直接用-l参数就能链接了,但如果库文件没放在这三个目录里,而是放在其他目录里 阅读全文

posted @ 2010-11-10 11:20 究生 阅读(193) 评论(0) 推荐(0)

导航