[英]C++ Code for Windows & Linux
First of, I am very new to programming, but took an interest in it. 首先,我对编程非常陌生,但是对此很感兴趣。 I have successfully built a C++ Console program for Windows which is a simple Database program, which can edit / delete / input entries.
我已经为Windows成功构建了一个C ++控制台程序,它是一个简单的数据库程序,可以编辑/删除/输入条目。
I am less and less relying on Windows for day to day stuff. 我越来越不依赖Windows处理日常工作。 I had an old HP Netbook which was impossible to use with Windows, but I put in a Linux Distro and works like a charm.
我有一台旧的HP上网本,无法在Windows上使用,但是我装了Linux Distro,工作起来很吸引人。
As I sometimes do use Windows, as well as having built the program to use in Windows, I am wondering if the same code can be used to compile a Linux program? 当我有时确实使用Windows以及构建要在Windows中使用的程序时,我想知道是否可以使用相同的代码来编译Linux程序? I could use WINE to run it but would prefer running something specific to Linux.
我可以使用WINE来运行它,但更喜欢运行特定于Linux的东西。 Is this possible with the same code or would I have to make another Linux version of it?
是否可以使用相同的代码,还是我必须制作另一个Linux版本?
I would assume that since you are new to programming, that you did not make the extraordinary effort to make your code portable across platforms. 我假设由于您是编程的新手,因此您并未付出巨大的努力来使代码跨平台可移植。 That takes a significant skill set, especially if you are accessing external resources such as a database.
这需要大量的技能,尤其是在访问外部资源(例如数据库)时。 So the answer is you will probably have to re-write for Linux, and specifically the database interface.
因此,答案是您可能必须针对Linux(尤其是数据库接口)进行重新编写。
I guess that you want your C++ code to be compilable both on Linux and on Windows. 我猜想您希望C ++代码在Linux和Windows上都可以编译。 You'll need operating-system specific compilers for that (a different one on Linux and on Windows).
为此,您需要特定于操作系统的编译器(Linux和Windows上的编译器不同)。
I am wondering if the same code can be used to compile a Linux program?
我想知道是否可以使用相同的代码来编译Linux程序?
The program to compile your C++ code is called a compiler . 用于编译C ++代码的程序称为编译器 。 On Linux you will use GCC as the
g++
command (which you could even customize with MELT , but that is not for newbies), or Clang/LLVM as the clang++
command. 在Linux上,您将使用GCC作为
g++
命令(甚至可以使用MELT进行自定义,但不适用于新手),或者将Clang / LLVM作为clang++
命令。 You'll use a builder like make
(see here for why, and this example). 你会使用像一个建设者
make
(见这里为什么,和这个例子)。 Be sure to install a recent version (GCC 4.9 or Clang 3.5 at least in start of 2015) to get good C++11 support. 请确保安装最新版本(至少在2015年初使用GCC 4.9或Clang 3.5)以获得良好的C ++ 11支持。 Focus on learning C++11 (or C++14) not some earlier variant (so use a C++11 compiler).
专注于学习C ++ 11(或C ++ 14)而不是早期版本(请使用C ++ 11编译器)。
I don't know Windows so I cannot recommend any good C++ compiler for it (but I heard of MinGW , CygWin and of Microsoft Visual C++ ; look also into recent Clang ...). 我不知道Windows,所以我不能为此推荐任何好的C ++编译器(但是我听说过MinGW , CygWin和Microsoft Visual C ++ ;也请参阅最近的Clang ...)。
You might be interested in cross-platform C++ framework libraries like Qt or POCO (and perhaps also Sqlite for database related stuff). 您可能对跨平台的C ++框架库(例如Qt或POCO)感兴趣,也许还对与数据库相关的东西使用Sqlite感兴趣。 They will help you to code some C++ usable on both systems (after recompilation).
它们将帮助您编写在两个系统上都可用的C ++(重新编译后)。
BTW, you can always encapsulate your system specific code with preprocessor directives à la #if LINUX
;take care of putting all the OS specific (or OS related) code in a few source files. 顺便说一句,您始终可以使用预处理器指令àla
#if LINUX
来封装系统特定的代码;请注意将所有特定于OS(或与OS相关)的代码放入几个源文件中。
It could happen (and I wish that for you) that you get fond & happy of Linux and will, in a few months, prefer to code for Linux only (you'll then install Linux on all your machines). 可能会发生(我希望对您而言),您会对Linux感到满意和高兴,并在几个月后更喜欢只为Linux编写代码(然后在所有计算机上安装Linux)。 BTW, study the source code of existing free software you like and use on Linux.
顺便说一句,研究您喜欢并在Linux上使用的现有免费软件的源代码。 That will teach you a lot.
那会教给你很多东西。
The advices I gave here and here are still relevant today when coding on Linux. 今天,在Linux上进行编码时,我在这里和此处给出的建议仍然有用。 Read also something about porting & portability , and Advanced Linux Programming .
还请阅读有关移植和可移植性以及高级Linux编程的内容 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.