Hello :D; Today, I'd like to explore the usage of MinGW in the context of building a simple program that utilizes an important library in the compilation process. This tutorial will help you use MinGW to build a basic
program with the g++ command,
teaching the purposes of some of the commands, and even exploring how to
initialize WindowsSockets, or WinSock. Sockets are relevant to
networking, and learning to work with them means learning how computers
communicate through networks, with which much of our modern economy now
depends. So learning how to initialize this library for use in you programming
efforts will help considerably, and with networking becoming such a crucial
aspect of programming any application in the modern world, it should be
considered good knowledge as a programmer to know how a networking library
works, and how to use one.
First, load up the console of your choice and move to a
suitable directory, preferably an empty one. Make sure MinGW is installed; if
not, please refer to my previous tutorial for an example installation of MinGW
for Windows 7. From here, create a new cpp file with the following command : notepad newfile.cpp.
This will open notepad and
generate the filename your typed in, as a c++ file. Copy the following
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
WSADATA wsaData;
int main () {
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
printf("WSAStartup
Successful");
return 0;
}
This code simply creates a variable for holding the details
of the WindowsSocket implementation being run, wsaData. That variable is called in the WSAStartup() method, and if
unsuccessful, the program will output the “WSAStartup Failed” message and
return from main, ending the run for the program. If the parameters are good
for WSAStartup(), then the method will return a 0 to iResult and the if loop
will conclude. From here, the program will output a “WSAStartup() Successful”
message, then conclude the program with successful execution, by returning 0.
After the code above is in the .cpp file, save that file and
close it. From here, Type in the following sequence:
g++ -Wall newfile.cpp –o programName.exe –lws2_32
This will produce a simple program from the supplied file
from above. In this case, the above program will be as simple program that
outputs a message, but depending on the results of that program, it might be an
error message, or a successful initialization message for the Winsock library.
G++ : The C++
compiler installed with MinGW; this compiler and linker will create the program
executable for us, and is key in accessing any of the other variables.
-Wall : A Preprocessor flag known as W-all,
will enable all ‘W’ flags, enabling the compiler warnings and checks often
found in Visual Studios.
newfile.cpp : Our source file that will be compiled for our
program.
-o : Output
files, which can be specified, as we have with the .exe suffix.
-l (-lws2_32) :
This is a linker flag for compiling libraries with our program. In this case,
the –l is indicating a library to link with, and WindowsSocket, or ws2_32 in our command line argument,
indicating ws2_32.lib.
This should produce an executable for you to run in your own
prompt, which should be enough of a program to output the successful
initialization message in the command prompt.
No comments:
Post a Comment