c++ source code on linux server and windows client? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
This may be a really dumb question, but I hope to be able to clearly express myself.
My daughter has a multi-player game source code. For years, she's used Visual C++ to compile the game for Windows. There is a server exe file and a client exe file, which works well if using a Windows server.
What I want to know is, is it possible to compile the server file to use on a linux server and keep the client exe files for windows? Or would there be a compatibility issue?
Thank you.

What I want to know is, is it possible to compile the server file to
use on a linux server and keep the client exe files for windows?
Yes, it's possible. In fact this sort of thing is done all the time. I do it for a living. In order to accomplish this, you must do two things:
Write the code in the first place using only Standard-compliant idioms, syntax and functions. This can also be accomplished by using 3rd party libraries that are compatible with both Windows and Linux, such as Boost.
Compile the code on the target hardware.
Step 2 is arguably the easiest. All you need is access to a linux machine with a linux compiler, such as g++, and let 'er rip. You'll need to set up your own makefiles and such in most cases. Usually a tool such as CMake is used to accomplish this.
Step 1 is much harder. Code that is legitimately compilable on multiple platforms (such as Windows & Linux) is called "portable." Writing portable code is easy enough if you are focused on doing this from the outset. Retrofitting portability, on the other hand, can be a gargantuan task. Many projects are abandoned completely because of this hurdle. If your daughter hasn't been writing portable code from the start, there's a very good chance that making it portable is a near-impossibility.
I'm not trying to dissuade you from the attempt. In fact I'd encourage you to take it on. In my experience, making a platform-specific piece of software portable is one of the harder things for a programmer to do correctly, but even if the project is ultimately a failure the effort reaps its own rewards by making them a better programmer. In order to write portable code one needs to have a fairly solid grasp on the language at a brass-tacks level. Programmers who can write portable code are very often (not always) more skilled than their platform-specific counterparts.
As far as compatibility issues are concerned, the only real 'gotcha' that comes to mind is endianness. Especially in a client/server type system, you'll need to tacke this sooner rather than later.

Hard to say, C++ compilers are hardware specific (as opposed to Java), Your best bet is to try and just compile it on a linux distro C++ editor. See what errors you get and troubleshoot from there...
Good Luck

It depends. I doubt you will be able to port that Windows code directly to Linux and just recompile. The IPC (Inter-process communication) between the server and client is likely written using Windows procedures and would have to be changed for Linux. I'm fairly certain they use different calls for different platforms.
Of course, if you have the source you can make these changes yourself but it will likely require some debugging.
Info on Linux sockets: http://www.linuxhowtos.org/C_C++/socket.htm
Info on Windows sockets: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx

There are several approaches you might take. Each has a unique set of tradeoffs and benefits.
Rewrite the code to compile on Linux using native services. Difficulty +++, benefits +++
Rewrite the code to use a platform-independent library which provides needed services. Difficulty ++, benefits +++
Run the .exe code in a Linux-hosted virtual machine running Windows. Not difficult, benefits ++
Run the .exe on Linux using Wine a compatibility layer which translates Windows calls into Linux equivalents. For many properly written Windows programs, this works quite well. Not difficult, benefits ++
Each of these requires some degree of education either for learning a new programming environment or determining proper configuration and changes in operability. There are plenty of free forums for support for all.

Related

How to utilise all cores for C++ program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I translated a Java program into C++, and I was getting very slow performance from C++ as compared to Java. Looking at the activity monitor, I saw that the Java program, which I ran first, was using over 90% of the CPU, while the C++ program was using around 30%.
It appears that the JVM is able to harness the full power of the CPU without user intervention. What changes should I make to use the CPU better for the C++ case?
Read a good C++ programming book then see this C++ reference. Read also the documentation of your C++ compiler (perhaps GCC).
Read also some operating system textbook.
Consider using frameworks such as POCO or Qt or Wt.
With a multi-core processor, you might use C++ threads. You'll need to synchronize them, e.g. using mutex and condition variables.
On GNU Linux, you could read advanced linux programming and use the syscalls(2) related to process creation and management (e..g. fork(2), execve(2), waitpid(2)...) and profiling (see time(7), profil(3), gprof(1), perf(1)).
On Windows, you need to use the WinAPI.
I translated a Java program into C++
In general, that is a bad idea. Learn your programming language and your operating system, and design your software for it.
Take inspiration from existing open source software on github or gitlab (e.g. RefPerSys, Firefox, Tensorflow, OpenJDK, Clang static analyzer, Fish ...)
Java implementations (e.g. JVMs) have some garbage collection and some class loader.
C++ implementations do not have any garbage collection. So read the GC handbook. Notice that circular references are hard to handle efficiently in C++, and this could explain your performance issues.
On Linux/x86-64, you might load plugins with dlopen(3) with dlsym(3) (or generate machine code at runtime using asmjit or libgccjit)

Can MinGW replicate most unix system calls with no effort?

Background: I'm working on porting a large project that was developed in C++ exclusively for unix systems, to be compatible with windows to make way for an eventual windows distribution. I don't have very much experience in Windows development, but I'd like to get whatever I can do, done right, before senior developers move in and take over.
Question: So for a while, I've been looking up Windows versions for all the unix/posix calls that are used in the software, most coming from dirent.h, unistd.h, and some ones under sys, like sys/stat.h or sys/types.h. And although it'll take a lot of work to modify the programs to adopt the new Win32 API calling conventions and return types (and sometimes all new functions), it'll probably work, eventually.
But I've been seeing this come up frequently, the fact that MinGW, I guess, includes many native unix calls and functionalities as part of the GCC environment, and can translate them into windows-compatible calls so you can compile on Windows, and use said compiled program on Windows easily. In fact, one of the similar questions I read in the sidebar talks about just that. What I don't understand, and seem to have trouble understanding, is exactly the extent of this built-in translation functionality, and where I can find a list of system stuff that'll work with this.
Sorry this post is a little unstructured and that I'm so green with this, but I only have 2 weeks to try to accomplish something with this before I get swapped with a senior developer.
No. MinGW does not attempt to implement UNIX functions on Windows. It cannot replicate most system calls with no effort.
However, Cygwin does do that.

Is it possible to make C++ platform independent by making it run inside a VM just like in Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Given that Java is highly portable and it is not having serious overheads, can't C++ be made platform independent?
Yes, it is perfectly possible. For example, you can compile C++ to JavaScript ( see https://softwareengineering.stackexchange.com/questions/197940/how-to-run-c-code-in-browser-using-asm-js ) or to CLI byte code ( https://en.wikipedia.org/wiki/C%2B%2B/CLI ) to run on Windows or Linux, or various other targets.
None of these currently performs as well as native C++, and most lack direct access to operating system resources. So the portability comes at some cost, and usually if you wanted to pay the cost of targeting web browsers or CLI, you have languages better suited to those platforms.
In reality, the method of code execution (whether the code is compiled, interpreted, run by VM, etc) is more a property of the implementation, and not the language.
when people say C++ is a compiled language and that JavaScript is an interpreted language, that does not necessarily mean that you can't write a compiler that translates JavaScript code to machine code on your hardware of choice, but rather what is a common way to provide implementation for said language.
In practice, C++ is used because of its efficiency and close to the metal features that is a good choice for performance critical tasks like embedded systems programming, systems programming, graphics, etc, so getting C++ to run in a VM would defeat its purpose.
kinda like buying a fillet Mignon and cooking it in the microwave.
Java compiles to an intermediate platform-independent byte code that is then interpreted at runtime by platform-specific JVMs. That is what allows Java to be portable. Each type of JVM is tailored to run the byte code on the platform's particular hardware architecture.
C/C++ compiles to native machine code that runs directly on the CPU (or as directly as the OS will allow). So no, you cannot compile C/C++ in a platform-independent manner. You have to use platform-specific compilers to compile C/C++ code for each hardware architecture that you want to run your code on.

C++ and Lua from USB [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So about 2 weeks ago, I started learning C++ and Lua, and I would like to be able to:
compile C++ code (it would be a nice bonus if i could have a C compiler as well, as that's next on my list of languages to learn)
interpret (is that correct terminology?) Lua, and
do all of this from my usb drive WITHOUT downloading anything from the internet or changing the path variable. (I will mostly be working on school computers.)
As a side note, I have fallen in love with Sublime Text 2 (the portable version of which is already on my usb drive). If it's ABSOLUTELY necessary, I'll make do without it, but I would prefer being able to use it wherever I go.
Please be patient with me, as i have mentioned before, I just started learning how to program, and I have little to no knowledge on how things work. I have seen similar questions, but they never seem to help me much due to my limited knowledge, so PLEASE don't mercilessly close my question like others I have seen on this site.
Thank you in advance!
I recently added a page on Lua WIKI (great source of info) that may help you. It is a tutorial for complete newbies on how to build Lua from the sources using only free and "portable" (in the sense of "can be put on usb drives") tools. It is meant for Windows OS users. Do not forget to check the official getting started page and the main Lua site as well!
The fact that you cannot download anything is quite restrictive (how could you get a free compiler then?). Anyway as greatwolf mentioned TDM-GCC is a great C/C++ compiler for 32 bit x86 PCs. It is also patched to be perfectly "portable": I usually use it from a portable USB HD. The tutorial I mentioned shows you how to download it and "install it".
Note that although your sysadmin at school may have blocked your ability to change the path variable globally, you can set it for individual processes ("launched programs") using simple batch files (aka Windows command shell scripts).
Create a file named "myshell.cmd" with this content:
#set path=%path%;c:\the\path\to\my\app&cmd /K
the part c:\the\path\to\my\app must be the actual path of the directory (folder) where the application executable is placed. When you double-click on myshell.cmd a black box will open (assuming your sysadmin hasn't blocked this feature) where you can invoke the app executables.
For example, if you "installed" the TDM_GCC compiler in c:\myprogs\GCC inside that dir you will find a subdir named bin. That subdir must be put in the path, so your myshell.cmd file will be like this:
#set path=%path%;c:\myprogs\GCC\bin&cmd /K
Then in the "black box" I mentioned you can invoke the compiler typing:
gcc --help
As for your learning path, if you intend to learn both Lua and C or C++, I will advice you to try C instead of C++. C++ has more "high-level" features, but it is huge and although Lua can be used embedded in C++ code (of course this is an advanced topic anyway), it is designed to be directly embedded in a C application (it has an API which conforms to C conventions), so for a beginner probably the path Lua --> C --> C+Lua would be a bit easier. C in itself, although difficult to master, is a rather minimalistic language, so the information to digest about it is not that big.
Not to discourage you, but IMO both C and C++ are not the most suitable languages for absolute beginners (they are plenty of pitfalls and have almost no "safety nets" for beginners). But that's up to you, it heavily depends on your skills, dedication and motivation ;-)
Hope all this helps.
For windows,
Take a look at
http://nuwen.net/mingw.html
You should be able to extract the download to a usb directory. Then you can click on the .bat file open a command prompt with the correct path settings.
As a bonus, it already includes prebuilt boost, which will make your c++ use easier.
For the C/C++ piece would also recommend you start with C. Not for ideological reasons, just that it's a lot simpler if you're trying to work out the basics of compiling/linking etc.
As a first C compiler I would recommend the tiny C compiler
Tiny C Compiler
It's one of the simplest to get your head around that I've seen and you can still build lua libraries etc.
Once you're comfortable with that then progressing to one of the more powerful environments such as gcc under MingW or Visual C++ should be a bit less daunting.
Lua is trivial. Download the binaries, put them on your drive, and configure Sublime Text to invoke them on Lua files.
C/C++ is more complicated only because of the range of options is so vast. I use a 2003 version of Microsoft Visual C++, which covers my needs. I find a copy here.
Keep in mind that C++ is a vastly more complicated superset (non-strict) of C, so you're going to learn most of C in the process of learning C++. IMO, learning C first is better for a whole host of reasons. You'll hear some people argue the opposite, but in this case there's a clincher: Lua is written in C and its API is designed for C. Exposing idiomatic C++ (i.e. objects) to Lua is a big ball of complexity that you just don't need right now, while learning two languages.

Creating/Organising a portable C++ library

I'm not sure the way I'm organising my library is the most elegant way to organise it. I'm mainly concerned about making all the code that I type compile/run for all systems that I'm targeting (keeping it portable), and also keeping it up-to-date for every system.
For example:
I'm not sure using __declspec(dllexport/dllimport) would be used for Mac or Linux. I assume that it's not, but I don't know what the equivalent is for Mac/Linux is. Or another example might be calling specific Operating System functions, which I try to avoid. However, things such as: measuring how long something takes to happen*, in a precise manner, does require me to call OS specific functions.
*as in getting the user's time precisely (down to micro/milli-seconds).
The systems that I am currently (perhaps more in the future) aiming for is Mac, Windows and Linux. But testing that the code compiles (and runs correctly) for every system just seems like a waste of time. As currently, the way I'm proposing to do it, requires me to make a separate project for every system. i.e. I create a Visual Studio Project for Windows, an Xcode project for Mac, and use the command line for Linux or use some other IDE.
Okay, so my main problems with the way I organise things are:
1. Time Consumption, as in creating the projects for all systems and keeping each indiviual project, for each system up-to-date.
2. Knowledge on all IDE's/Compilers that I require to use
Please note, I've never really used* Linux before, I'm thinking about switching. The only problem that I'm concerned about is finding the right tools for me to use with it, and finding the right Distro that would suit me, for what I need. I would really appreciate if someone that is experienced with Linux could guide me, or give me some advice whether to switch or not.
I really like Visual Studio, and it's been my main IDE for quite a while now. I'm just not sure if I want to ditch Visual Studio or not, for Linux; as I don't know if the tools that are available for Linux can do what Visual Studio can or not. What I mean is, it being as user friendly as Visual Studio is. I'm afraid to learn how to use Linux's tools, I'm just not sure if it's worth doing so. Time isn't a major factor on this library, I have plenty of time, I'm fairly young and determined to program as my career in the future.
*I have used it before, but I've never replaced it for Windows.
I am currently hosting all my source code for my project on BitBucket, but at the moment, I only have the RAW code on my repo. There is no project files or any other tool to compile it with, just the code and a readme file. I was thinking of using Makefiles, since they seem popular. But I've never made a Makefile before, don't get me wrong, I am willing to learn. I'm just not really sure where to start. I've heard that people use CMake to create portable libraries, such as SFML and Ogre3D. I've built a couple of libraries with CMake, but I have no clue on how to actually make my own library with it to make my project/make files. Should I learn and incorporate CMake with my library, or is there a better option available?
EDIT:
I'm not aiming to write a library for actual Software that uses a GUI. I'm mainly aimed to write games.
1 - Boost. Boost will help your portability more than you can imagine. Its only real sticking point is, believe it or not, OS X.
2 - Use CMake. It integrates with Visual Studio project files as the build tool, and you can put most of your different-platform compilation voodoo in there.
3 - If you're seriously writing a portable library, consider writing it in C/writing a C wrapper, or making it header-only, or providing the source-code. Making it a shared or statically linkable library does not mean that it will play nice. Name mangling leads to inconsistencies that will blow your mind.
4 - Always be explicit about the number of bits in each variable.
5 - use git. It'll allow you to setup a crappy local server for a repository very easy and get very fast transfers of the kind of huge changes MSVC will make annoyingly
There are a lot more best-practices that can be discussed about cross-platform development. All of that advice isn't applicable in every case; I have a very code-heavy Linux/Windows library that I code almost exclusively in MSVC2k10 and mostly build/test for in Linux, and it is nowhere close to header-only.
EDIT in response to comments:
git was suggested because I find it very easy to use and manage locally. I've use svn before and liked it, I won't really endorse any others, but there are probably plenty of fine ones.
To expound on point 3,
A C wrapper would make it so that anyone anywhere could use your library - FORTRAN developers, Ruby, even Java.
Otherwise you generally have to have similar compiler versions to link properly and it will only link to other C++ code, outside the case of DLLs, and there are still versioning issues. It's one of the stupidest problems in C++ left over, check "name mangling" on Wikipedia. There is a reason widely-used libraries are written in C or have C wrappers, such as libz, openssl etc.
There are other advantages to it. Exception propagation across dynamic libraries is non-existent; with static libraries it can be inconsistent or non-existent.
You'll find that the most widely-used C++ libraries are mostly header-only, like Boost. A header-only library solves many problems by putting all the code directly into a project in a relatively intuitive way, and modern compilers can still optimize away much (but certainly not all) of the extra compile time associated with it.
With all this said, it is certainly possible to do without a C wrapper or header-only, it is just annoying and very troublesome. DLL hell and its Linux equivalents still exist.
You also asked about Boost. That depends. If you're distributing the sourcecode then you certainly must distribute Boost with your code/have people install it. Having people install libraries in order to compile other libraries or use programs is common practice. Think of how specific versions of DirectX come with games for an example.
However if you are distributing binary versions of your library, statically linking against Boost will eliminate any need to include it as long as you are careful to keep Boost headers out of the outward-facing parts of your library. This is where you start seeing things like void * pointers inside C++ headers; an unfortunate side-effect of some of the shortcomings of C++ compilation and library distribution unfortunately.
CMake is a good choice. You can learn to use it. Read a tutorial:
http://www.cmake.org/cmake/help/cmake_tutorial.html
But, if your targets are Linux and Windows only. It is probably OK, in your case (small/average first multi-platform project), to maintain 2 separate build systems.
On Linux, Use Make. It is standard and has a very good reference manual:
http://www.gnu.org/software/make/manual/make.html
On Windows. Use your IDE project file, be it Visual, DevC++ or other. That is the simplest way to go.
Most important, make it easy to test your library/software on different platforms. Install a virtual machine on your desktop. Or at least compile your library into Cygwin.
Once you are here come back on stackoverflow and we will help !
Personally I'd leverage a framework like Qt, because it is quite portable, it does abstract a lot of OS functionality (files, timing, threads, networking), and you get a decent, free IDE (Qt Creator) that is also portable and runs on Windows, OS X and any Unix flavor that runs Qt. It'd give you the lowest barrier to entry. Qt Creator can leverage the Visual Studio compiler and the CDB debugger if they are available.
You do not need to use OpenGL to use Qt, in fact you're not bound to any particular graphics subsystem. Qt only "uses" OpenGL in Qt 5 for the Qt Quick 2 graphics backend. It's not needed for Qt 4, nor for Qt Quick 1 (even in Qt 5!).
You can use any 2D or 3D framework you fancy to push images and other content to the screen. What Qt is good at is creating the kind of 2D imagery that is often needed in games - menus, configuration screens, HUDs, etc. There's a lot of controls and drawing primitives that Qt makes easy to leverage for your purposes.
Qt also lets you use a reasonably powerful model-view and networking frameworks, thus you'd be able to reasonably easily generate server or client lists that update in real time.
There'd need to be a small amount of shim code between Qt and DirectX, of course. On the output side, you typically end up with a QImage in Qt, and then use DirectX, SDL, OpenGL, etc. to push it to the screen. On the input side, you need to call qApp->processEvents() within your main game loop, and you will need to post user input events from DirectX etc. to Qt's event queue using qApp->postEvent(...). This would be only needed if, say, DirectX main loop consumes all Windows messages and won't let standard winapi/win32 code (Qt's windows event dispatcher) see them. I haven't deal with DirectX much, so others feel free to chime in with details, of course.