可以从C标准库函数在C ++中使用? [英] Can functions from the C standard library be used in C++?
问题描述
现在我越来越熟悉C和C标准库,我不知道当我打开使用C ++的工作在以后的时间我在这方面的知识是有用的。
Right now I'm getting familiar with C and the C standard library and I wonder if my knowledge in this area will be useful when I turn to working with C++ at a later time.
因此,我想知道,我是否可以使用C标准库提供的功能在C ++环境,以及是否和为什么它会是有意义的其实这样做。
Therefore I'd like to know, whether I can use the functions provided by the C standard library in a C++ setting, and also whether and why it would make sense to actually do so.
推荐答案
是的,C ++最初的设计,使任何C语言库可以在C很容易地使用++。当然,这是略少真(特别是,如果C库恰巧使用像一些C ++的关键字尝试
或的dynamic_cast
,它不会工作;同时,如果回调 $ C $在C ++ CD传递给C语言库产生异常,你很可能有一个大的混乱)。
Yes, C++ was originally designed so that any C library can be easily used in C++. Of course this is slightly less true (in particular, if a C library happens to use some C++ keyword like try
or dynamic_cast
, it won't work; also, if a callback coded in C++ passed to a C library is raising some exception, you are likely to have a big mess).
在C ++中使用C头文件中的标准做法是
The standard practice to use a C header file in C++ is
extern "C" {
#include <some_c_header_file.h>
};
和大部分的现有的C头文件旨在与C ++合作,通过实际包含的东西,如
and most existing C header files are designed to cooperate with C++ by actually containing stuff like
#ifdef __cplusplus
extern "C" {
#endif
//// most of the header material goes here, C style
#ifdef __cplusplus
}; // end extern "C"
#endif
在实践中,许多C标准头文件具有同等的C ++头包之类的东西上面(也包括空间std
)。例如c &LT; stdio.h中&GT;
是C ++ &LT; cstdio&GT;
- 但你要经常preFER正品C ++流(&LT; iostream的&GT;
),但的printf
式的程序通常都比较的localization 的友好夹杂着的gettext (3)。
In practice, many C standard headers have equivalent C++ headers wrapping things like above (and also in namespace std
). Eg C <stdio.h>
is C++ <cstdio>
-but you often should prefer genuine C++ streams (<iostream>
), however printf
-like routines are usually more localization friendly mixed with gettext(3).
然而 C和C ++是非常不同的语言您应该code在习惯C ++ 11(使用标准C ++的containers ,汽车
的关闭的, RAII ,的智能指针,五的规则, SFINAE ,例外一> 匿名函数的,...)
However C and C++ are very different languages. You should code in idiomatic C++11 (using standard C++ containers, auto
, closures, RAII, smart pointers, rule of five, SFINAE, exceptions, anonymous functions, ...)
一些标准的C函数不在地道Ç非常有用++。例如,你是不太可能使用的直接的的malloc
中的真正的C ++(至少preFER 新
哪位还是非常低的水平,并没有更多的C ++神魂,更可能使用了大量的容器和智能指针的没有的堆与手动处理分配)。但 POSIX 功能(特别的系统调用(2) ....)在C非常有用++。 的longjmp
很可能是不符合C ++异常。
Some standard C functions are not very useful in idiomatic C++. For example, you are unlikely to use directly malloc
in genuine C++ (at least prefer new
-which is still very low level and no more in the C++ spirit-, more likely use a lot the containers and the smart pointers without dealing manually with heap allocation). But POSIX functions (notably syscalls(2) ....) are quite useful in C++. longjmp
is likely to be incompatible with C++ exceptions.
BTW,C ++已经发展的很多的在本世纪。不学C ++ 98,但至少 C ++ 11 (还有的巨大的也许 C ++ 14 它们之间的差异)。使用的最近的编译器( GCC 或的锵/ LLVM );在2015年12月,这意味着 GCC 5 至少还是的Clang/LLVM 3.7 最少。不要忘记,以使所有的警告和放大器;在编译调试信息(例如 G ++ -Wall -g -Wextra -std = C ++ 11
)
BTW, C++ has evolved a lot in this century. Don't learn C++98 but at least C++11 (there are tremendous differences between them) and perhaps C++14. Use a recent compiler (GCC or Clang/LLVM); in december 2015, that means GCC 5 at least or Clang/LLVM 3.7 at least. Don't forget to enable all warnings & debug info in the compiler (e.g. g++ -Wall -Wextra -g -std=c++11
)
C ++ (这意味着至少有C ++ 11)的是困难的编程语言后,相当多的复杂的比C是。你需要几个星期的阅读,了解一些了吧,和良好的编码风格和纪律是必要的(你可以很容易写的很蹩脚code在C ++中)。开始的编程:原理与放大器;练习使用C ++ 的
C++ (that means C++11 at least) is a difficult programming language, considerably more complex than C is. You'll need weeks of reading to learn some of it, and good coding style and discipline is essential (you can easily write very crappy code in C++). Start with Programming: Principles & Practice Using C++
我相信,如果你只知道C,阅读 SICP (和研究了一下计划)<前EM> 的学习C ++是值得的。
I believe that if you only know C, reading SICP (and studying a bit of Scheme) before learning C++ is worthwhile.
未定义行为的概念是非常重要的,无论是在C和用C可能更加++。你绝对需要了解它(见它C.Lattner的博客一>)和避免它。
The notion of undefined behavior is very important, both in C and probably even more in C++. You absolutely need to understand it (see C.Lattner's blog on it) and avoid it.
您也将通过研究(或许促成)某些现有的免费软件及其来源$学到很多大C $℃。因此,我建议使用Linux操作系统。
You will also learn a big lot by studying (and perhaps contributing to) some existing free software and its source code. Hence I recommend using Linux.
这篇关于可以从C标准库函数在C ++中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!