inet_pton in QT creator for both windows and linux - c++

i am writing a QT/C++ program, that i don't want to have dependecies on a specific OS (at least keep them to a minimum with some preprocessor directives).
i'm using QT Creator to design my program, which is done mainly on my Linux box, and every now and then, pull the code on the windows machine and attempt to compile it to make sure it still compiles.
to the point now, i need to use the functions inet_pton and inet_ntop, but they seem to be part of the GNU C Library. I looked around, and read that some MS libraries have them as well, though i'm no expert in MS development.
my question, is there an easy way to use these functions in my program? or am i better off checking out their code in glibc, and implementing them myself? or can you recommend some other alternative?
thanks in advance

inet_pton/etc is only available on Vista and above. If you need to run on WinXP you can use WSAStringToAddress().

Related

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.
I am less and less relying on Windows for day to day stuff. 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.
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? I could use WINE to run it but would prefer running something specific to Linux. Is this possible with the same code or would I have to make another Linux version of it?
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.
I guess that you want your C++ code to be compilable both on Linux and on Windows. You'll need operating-system specific compilers for that (a different one on Linux and on Windows).
I am wondering if the same code can be used to compile a Linux program?
The program to compile your C++ code is called a compiler. 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. You'll use a builder like make (see here for why, and this example). 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. Focus on learning C++11 (or C++14) not some earlier variant (so use a C++11 compiler).
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...).
You might be interested in cross-platform C++ framework libraries like Qt or POCO (and perhaps also Sqlite for database related stuff). They will help you to code some C++ usable on both systems (after recompilation).
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.
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). BTW, study the source code of existing free software you like and use on Linux. That will teach you a lot.
The advices I gave here and here are still relevant today when coding on Linux. Read also something about porting & portability, and Advanced Linux Programming.

How can i make a portable C++ program for Windows with GUI, independent from .NET or other Libs?

I want to make a C++ program with Graphical User Interface, to be running on Windows platforms.
I want it to be independent from any libraries like .NET.
The reason I want this is because I want this program to be able to run everywhere, in every version of Windows (XP, VISTA, 7 - 32 & 64bit) without the user to be bound to have already installed a library like .NET.
Also, I want it to be portable: the whole program to consist from only one exe file.
So to summarize:
How can I make an one-exe-file GUI C++ program, able to run on all versions of Windows, without the need of pre-installed libraries on Windows from the user that uses it?
I have already MS Visual C++ 2010 Express and Eclipse with Cygwin's G++ compiler installed on my system.
Notice: I don't mind to use any libraries for windows GUI design if there is a way to embed them inside the executable file of the program.
You can either use a library that can be statically linked (MFC, WTL) or use Win32 API directly.
WxWidgets have static linking capability, never tried it myself.
If you want portabiliy, and yet don't want to depend on 3rd party libraries... well you'll have to provide portability yourself! Don't do it! As adviced by OneOfOne, use Qt and be impressed how simple yet powerful it is. Good luck!
You can also use Qt5, works great on windows and when you figure out that windows isn't the only OS out there, you can easily port it to everything else.
License issues : Proprietary Source code + LGPL Source code

Building C++ cross-platform static libraries

I want to build cross-platform statically linked library in C++.
How am I supposed to do this(I am a complete NOOB, never been using g++,gcc,mingw etc I looked on the internet for a solution to my problem, but didn't find anything. Except that mingw is a Minimalist GNU for Windows...)? I've been always using Visual c++ on windows, but now I have to build lib which will be attached to Qt projects for: windows,mac,linux.
If anyone know a good tutorial, on using compilers, and how to build libraries, I'll appreciate sharing them here :)
Thanks, may the force be with you.
MinGW is a very good compiler! I use it all the time. Even though MinGW stands for Minimalist GNU for Windows, MinGW has everything you need to program in either C++ or C. If you are creating a QT Application, I recommend using QT Creator (just google it). I haven't used it lately, but I think it supports Cross-Platform compiling (as does MinGW, with some command-line commands...).
The best way to cross-compile though, is but getting a copy of each OS that you are going to compile for. So if you are doing Windows, Mac, and Linux, I recommend getting one of each (Mac OS X is around $25 and Linux is free). Then just use the included compilers (XCode on Mac; GCC on Linux) to compile. Unfortunately when you are performing cross compiling, you need to support each OS individually (some parts can shared between OSes).
Since you will be using a toolkit (QT), you probably don't have to worry much about platform specific calls or anything. Just make sure you use the QT typedefs and structs and not the Microsoft Windows ones...

Writing C/C++ with Unix Api in Visual Studio?

I'm a third year computer science student. I was raised on Visual C++ and have gotten quite proficient at using it. My school however, teaches primarily on Linux platforms. Up until now I have just programmed and debugged in Visual studio, then when I was certain I got everything working, I would recompile the source in Linux to make sure it work there.
Now however now my projects require use of the Unix API calls, Berkly sockets and sometimes pthreads.
Are there libraries available that give me access to the Unix API on windows? If so how would i go about using them Visual C++ 2010?
I really don't want to have devolve to using Gedit and Gdb for debugging complex(for me at least) code.
Typically, you would just use the platform-independent equivalents found in various libraries- usually Boost. However, if you must use the Unix-specific APIs, the only way to go will be to wrap their functionality for Windows. Likely, there already exist libraries that serve this purpose, much as you can use WINE and such for Unix, but as a Windows programmer myself I wouldn't really know.
You could try something like Cygwin to provide the Unix API in Windows, build and compile there, then make any minor adjustments to use the native API when you transfer to Linux.
You can also check each library to see if a Windows version is available. If no Win version is listed on the lib's site, Google around and see if someone has built it for Windows; that may provide you a usable version.
If you don't specifically need the Unix API, Boost or another cross-platform library is probably your best bet to provide the needed functions.
For non-Windows development, finding a replacement to VS is probably a good idea (unfortunately there's really no IDE that can compare, IMO). Code::Blocks is the nearest, but is still missing a lot of features. KDevelop has been recommended to me several times, seems nice at first glance. Eclipse is maybe the only IDE slower than VS2010, something to consider before using it.
Most Unix-only APIs aren't available for Windows. I suggest you switch to Code::blocks, which is cross-platform. It is pretty similar to VC++ in many ways, and it's easy to get used to it.
Either switch to Code::blocks, or switch to Eclipse - both of which are multi-platform. I can attest that Eclipse 3.6 works awesome debugging C++ Qt apps on Linux and Windows.
I know this is late, but a good answer is never too late! Hopefully mine is one.
The only thing I can think of is use the preprocessor.
#ifdef __unix__
...UNIX code here
#endif
and write the UNIX code in the block so that you can compile it on the UNIX system required. Then, outside those blocks you could use the WIN32 analogs inside a similar block
#ifdef _WIN32
...Windows code here
#endif
Such that an end result may look like
//Cross platform includes
//cross platform namespaces
#ifdef _WIN32
//Windows includes
//Windows namespaces
#else
//unix includes
//unix namespaces
#endif
int main()
{
DoCrossPlatformFunctions();
#ifdef _WIN32
DoWindowsFunctions();
#else
DoUnixFunctions();
#endif
DoMoreXplatformStuff();
return 0;
}
You could do some short research and find the analogs to the functions you require fairly easily and then be on your way. It's a bit cumbersome, but since you seem to have a vendetta against just writing it in a UNIX VM in the first place it's probably the best option. The best part is you don't have to step outside your Visual C++ comfort zone to do it. I hope this helps!!

Desktop Development Environment that Compiles to Linux, Mac OS, and Windows

is there any development environments that allow you to have one code base that can compile to Linux, Mac OS, and Windows versions without much tweaking? I know this is like asking for where the Holy Grail is burred, but maybe such a thing exists. Thanks.
This is achieved through a number of mechanisms, the most prominent being build systems and specific versions of code for certain systems. What you do is write your code such that, if it requires an operating system API, it calls a specific function. By example, I might use MyThreadFunction(). Now, when I build under Linux I get a linux specific version of this MyThreadFunction() that calls pthread_create() whereas the windows version calls CreateThread(). The appropriate includes are also included in these specific platform-files.
The other thing to do is to use libraries that provide consistent interfaces across platforms. wxWidgets is one such platform for writing desktop apps, as is Qt and GTK+ for that matter. Any libraries you use it is worth trying to find a cross-platform implementation. Someone will undoubtedly mention Boost at some point here. The other system I know if is the Apache Portable Runtime (APR) that provides a whole array of things to allow httpd to run on Windows/Linux/Mac.
This way, your core code-base is platform-agnostic - your build system includes the system specific bits and your code base links them together.
Now, can you do this from one desktop? I know you can compile for Windows from Linux and so probably for Mac OS X from Linux, but I doubt if you can do it from Windows-Linux. In any case, you need to test what you've built on each platform so my advice would be to run virtual machines (see vmware/virtualbox).
Finally, editors/environments: use whatever suits you. I use either Eclipse/GVim on Linux and Visual Studio on Windows - VS is my "Windows build system".
Maybe something like CodeBlocks?
Qt is a good library/API/framework for doing this in C++, and Qt Creator is a very pleasant IDE for it.
I've heard this is possible. Your compiler would need to support this. The only one that I know that does is GCC but it obviously requires a special configuration. I, however, have never used this feature. I've only seen that it exists.
What you are looking for is called "Cross Compiling"