简体   繁体   中英

Microsoft Visual Studio: Windows and Unix project source code compatibility

I'm going to develop a command line tool for both Windows and Unix OS architectures. However I'm facing a struggle. How can I make the code effectively transferrable between Windows and Unix?

Project is going to be made in C++ language using primarily MS Visual Studio. However I want my tool be cross-platform, so I can use it both in Windows and Unix systems. Since Unix doesn't support Visual Studio, I need to somewhat design the code structure to support both architectures (both being 32bit) and OS-specific headers need to be included for just the only one OS architecture the code is built in.

So I am asking for your help. What is the most effective way to maintain pretty much the same code for both Windows and Unix, with all neccessary headers for that particular OS? So I can create program.exe file in Windows and program.run file in Unix? Some conditions to test current OS architecture (Windows or Unix) and include OS-dependent headers?

What about encoding? Because Windows command line uses MS-DOS encoding (In Central Europe, it's DOS codepage 852), and Unix terminal uses UTF-8 encoding. My tool will be multilingual (with contributors, starting with English and Czech languages from my side).

I need to keep the code structure as small as possible, and of course easily maintainable. The source code shall be available for clone/fork from GitHub repository.

If this is not the kind of question apropriate for Stack Overflow, then just tell and I'll delete it. Just asking for help, nothing else.

Step 1) set up your continuous integration system to build the code on Windows and Unix every time from day one. Preferably with multiple compilers on each platform.

Step 2) Use a cross-platform build system like scons or cmake .

Step 3) Don't use any platform specific code in your program at all - and when you have to, implement multiple versions under various ifdef guards. Prefer using cross-platform libraries instead.

Step 4) make sure you have plenty of unit tests ( especially for the bits where you had to implement something multiple times using platform specific code) and make sure your CI system runs all the tests for every commit.

Step 5) Make sure all text is UTF-8 encoded from day one - conversion to other encodings can happen at the display layer, but keep everything UTF-8 clean internally.

Step 6) Do extensive manual testing on all supported platforms.

And make sure to tweak every compilers warning setting waaay up and make warnings errors (and fix all such errors). What's a warning with one compiler on one platform can often be silently miscompiled code with a different compiler on another platform.

First, the windows console can use utf-16 but not utf-8. Second, if you stick to the standard c++ library, possibly extended with boost (which will have to be installed both on windows and any *nix system you are going to build on) you really should not have any problems so long as you are talking about strictly accepting command line arguments performing work and printing results.

If you are talking about terminal control (that is, printing on the screen somewhere other than the next character cell, in particular going backward) this is going to be quite a bit more difficult.

I would, however, suggest that you use a cmake based project, Visual Studio normally uses the custom msbuild build manager and that is not going to help if you try building on a *nix system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM