需要在linux下写一个守护进程,不知道用什么C++或C
我在选择正确的语言来编写我的守护程序时遇到了一些问题, 我对 C 和 C++ 感到困惑,我想使用 C++,因为它比 C 更具扩展性, 但我想使用 C,因为它是 linux 中一切的起点,
我想选择 C++,因为我有很多关于它的资源,所以,如果我选择 C++ 而不是 C,会有什么区别吗?
如果我多学习 C 语言,我会有什么好处? 我觉得如果我进入 C++,我会在 C++ 中涵盖 C...
问候
I have a little problem picking the right language to write my daemon,
I am confused between C and C++, I want to use C++ because it is more expanded than C,
but I want to use C because it is the starting point of everything in linux,
I want to go for C++ as I have many resources about it, so, does it make any difference if I pick C++ instead of C?
and what I will have good if I learn C more?
I feel like if I go into C++ I will cover C within C++...
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用您现在最熟悉的语言。
Use whichever language you know best right now.
既不要使用 C 也不要使用 C++,一般编程并不真正需要它们。
使用使事情变得简单的高级语言,例如 Python
当然,这取决于你是哪种“守护进程”写作,但很可能,您希望将开发工作集中在手头的任务上,而不是修复内存泄漏、字符串处理或其他干扰等问题。既不使用 C 也不使用 C++(在较小程度上)将允许您执行此操作。
Use neither C nor C++, they are not really required for general programming.
Use a high-level language that makes thing easy, such as Python
Of course it depends what kind of "daemon" you're writing, but in all likelihood, you want to be focusing your development effort on the task in hand, not fixing things like memory leaks, string handling or other distractions. Using neither C or (to a lesser extent) C++ will allow you to do this.
我可以完全用代码来回答这个问题。编写守护程序大致包括以下操作:
这些都是 C 函数调用,但当然,您可以从 C++ 调用它们。
我之所以保留这段代码,是因为它实际上是一个函数,我将函数指针传递给它来执行我的“守护程序主体”。我为什么要这样做?为了找出我的错误直接作为进程运行代码的位置(如果需要使用root权限),然后我“守护进程”。否则调试守护进程是相当困难的...
编辑:当然,使用函数指针是一种 C 思维方式,但没有理由不能实现某种形式的基于类的机制。
所以说实话,这真的不重要。选择您喜欢的。
I can answer this one entirely in code. Writing a daemon roughly consists of doing this:
These are all C function calls, but of course, you can call them from C++.
The reason I have this code sat around is because it's actually a function I pass function pointers to which executes my "daemon body". Why do I do it this way? To work out where my errors are running the code directly as a process (if necessary with root privs) then I "daemonise". It's quite hard to debug a daemon process otherwise...
Edit: of course, using function pointers is a C way of thinking, but there's no reason you can't implement some form of class-based mechanism.
So, honestly, it really doesn't matter. Pick whichever you prefer.
就我而言,C++ 是具有可选附加功能的 C。因此,就这样吧,使用任何你觉得舒服的东西。
as far as I'm concerned, C++ is C with optional extras. So go with that, and use whatever bits and pieces you feel comfortable with.
如果您更喜欢 C++,则没有理由选择 C 而不是 C++,反之亦然。它们都是同样有能力完成这项任务的语言。
除非您希望更熟悉 C,否则只需使用您所知道的即可。
There is no reason to choose C instead of C++ if you are more comfortable with C++, and vice versa. They are both equally capable languages for this task.
Unless you are looking to become more comfortable with C, just use what you know.
除非您打算进行内核开发或嵌入式编程,否则学习 C++ 绝对比学习 C 好。您唯一需要注意的是 C++“破坏”了其函数名称,因此您的函数
void foo( )
C++ 中的 C 程序无法直接访问。诀窍是使用 C 链接来声明它,如extern "C" void foo()
中。也就是说,C++ 是一种比 C 更大的语言,并且肯定需要更多的时间来学习。
Unless you are planning on doing kernel development or embedded programming, learning C++ is strictly better than learning C. The only thing you need to be careful of is that C++ "mangles" its function names, so that your function
void foo()
in C++ will not be accessible directly by a C program. The trick is to declare it with C linkage, as inextern "C" void foo()
.That said, C++ is a much bigger language than C, and it will definitely take more time to learn.
你绝对应该使用 C++。
You should definitely use C++.
你绝对应该使用C,就这样。
You should definitely use C, period.