Cross-platform

I need to write a small program for the university. The problem is that it should be in C / C ++ under Linux, and I never used linux, I expect that I will have problems with the IDE, compilation and all that.

Is it possible to encode it under windows, and then "copy / paste" the code and compile it under Linux? What limitations should I know about whether this is possible?

This is a small program, typical client / server communication using sockets.

+6
c ++ c linux windows cross-platform
source share
12 answers

I think you should go and do it under Linux (gcc?). This will teach you some things about "old school" programming. Forget about using the IDE, use vim (if you already got one) or nedit (more like notepad).

Compile on the command line. Tie it yourself. Write a make file for this.

These are the basics. You need to understand this before using the IDE. Do this while you're still at university, because it's a pain, and you (and should) want to use the IDE for real work!

In addition, a basic understanding of Unix is ​​not difficult (I found my way around Solaris, Ubuntu, and OS X, coming from the Windows background) - you need to run some simple guides. To write small school projects you need little to know: cd , ls , mkdir , make , gcc (be sure to use g++ for C ++ projects - this has bitten me on my Mac before ...). Stay close to your home directory ( ~ ).

Running your project on the target system will help you understand some things correctly: when I ran these simple sockets and pthreads examples, I found a compilation and binding them to non-portable ones. On some systems, linking in libraries must be done this way, in other ways.

BTW . If you really want to do this under Windows, it is best to have a POSIX environment under Windows. POSIX sockets are different from the Windows network model, if I remember correctly.

Try either MinGW or Cygwin. Both should provide you with * nix development environment for Windows. You can use your favorite text editor (Windows vim port?) And cmd.exe instead of bash to run the compiler :)

EDIT : Sorry if the tone is confrontational (as per the comment). I will try to soften it a little. It's just ... I saw a lot of people trying to learn C / C ++ (or Java, for that matter) with the IDE, and came to the conclusion that they were stopping me from starting. Of course, you will need the best tools for real programs, but the overhead of project files, etc. For school pattern designs, add clutter. It also makes it difficult to email your homework to your teacher - a zip with a bunch of .c and .h files, and a make file is really as simple as ...

+26
source share

If you need to do encoding on Windows, I would recommend using mingw / msys as your development environment. Msys implements a unix-like shell on windows, and mingw is a port gnu compiler (gcc) compiler and other gnu build tools for windows platform. This is an open source project and well suited for this task.

The installation procedure may be a little complicated, but I found that the best place to start is here .

+8
source share

Technically, you can write a lot of code that will work with very few changes on Windows and Linux platforms.

However, in some cases, things get complicated. It is often easier to switch from linux -> windows than from windows -> linux, just because Windows users tend to use Windows API calls that don't have linux equivalents.

If you intend to use the graphical interface, you will want to use the cross-platform graphical interface. Qt or wxWidgets are good options. Qt, in particular, also has many cross-platform utility classes that go beyond the GUI space and can help with socket issues.

If your goal is to make it work on Linux, and the software should not run on Windows, I would recommend just jumping in and trying to make it work from the very beginning. This will be easier than trying to execute the port.

As for the Linux IDE - at some point plan to log in and use the Linux tools - you will need a different build system (if you are not using a cross-platform solution such as cmake or scons), there will be different IDEs if you are using an IDE, etc. d.

KDevelop is a good IDE that will be more familiar than trying to do everything from the command line if you are used to working in an integrated development environment.

+7
source share

Take a look at the QT library and tools

+5
source share

Instead of cutting and pasting, split your project into platform agnostic files and platform-specific files and / or use preprocessor directives to configure the code. Most of my work is for Windows, and it's nice to use it as a test bench for Linux using familiar tools.

+4
source share

First: Install Cygwin, this provides a UNIX-like environment for windows with a POSIX API that MingW does not provide, such as fork() . This way you can work on Windows and feel like on Linux.

Second: I would advise getting used to Linux / installing it, knowing that the Linux / UNIX environment will make your life at the university (and not only there) much easier.

+4
source share

If you can write a program using only the standard C libraries, then theoretically it should be cross-platform compatible, since the C library itself is designed for full portability. However, you will encounter socket issues which, if I am mistaken, are usually implementation specific.

Linux (especially for ubuntu) is actually not that difficult to use, but the lack of a real development environment for development usually leaves people out. I recommend that you compile with Cygwin for Windows and try the CodeBlocks code editor while you do this. Cygwin will compile your code for linux libraries instead of windows (and then emulate these libraries with Windows.dll when it starts) so that it knows your limitations well. You should be able to set up the CodeBlocks project under windows, and then open the same project under the Linux version of CodeBlocks and check your compilation.

Cross-platform code is not very easy to write, I admit, but it is more than possible with little effort.

+3
source share

Both Windows and Unix -enabled platforms offer Berkeley Sockets APIs , but the Windows implementation has some differences, for example. using the WSAStartup() and WSACleanup() functions. In addition, a few #ifdef and the application will be compiled on both platforms without any problems. Assuming this is, of course, a simple command line application. There is a section on Porting Socket Applications for Winsock on MSDN. All you have to do is work backwards.;)

Also read a book on socket programming, such as the Bej Network Programming Guide .

Another option is to use Boost :: Asio . This smooths out any differences in socket APIs very smoothly.

+2
source share

I agree with others that if you install something like Ubuntu Linux, it is not so difficult to find out. But to answer your real question, the main problem is the inadvertent use of the Windows API. My favorite environment for this is the AT & T U / Win toolet, which is designed to compile POSIX applications on Windows. It seems that developers have fewer complaints from developers than mingw, which in turn causes fewer complaints than cygwin.

+2
source share

If you adhere to C / C ++ standards, you should have no problem porting well-written code from windows to unix, with minor tweaking of the included headers, etc.

I assume that there is no GUI other than the command line since it was assigned client / server under Linux, which makes it possible. But be very careful to stick to unified socket styles and C / C ++ standards, rather than using any Windows libraries or shortcuts.

+1
source share

Juan

If you go crazy just learning your way around Linux, then you don’t need the patience to be a programmer. Coding is not glamor, it requires careful effort, reading documentation, trying to figure it out, clinging to a debugger for hours.

You need to either rethink yourself by participating in programming, or push it under it, and make an effort to learn how to navigate Linux, which is nowhere more difficult than any useful programming language.

+1
source share

If it does not use specific Windows API code or libraries without cross-platform functionality, and if it is standard C ++ - if it does not use specific Microsoft compiler functions - then it should be cross-platform and work with linux.

The linux compiler is called GCC. There is a version of Windows called MinGW . You can try before trying to use Linux. If it compiles on MinGW, it will certainly compile on Linux. (The problem is that MinGW is not updating with the latest versions of GCC ...)

To use libraries that are not cross-platform (possibly GUI libraries), you will have to modify them, but most non-OS GUI libraries are cross-platform anyway. For other libraries you will find equivalent on other platforms, I think.

0
source share

All Articles