Introduction

This software was written after hours of searching for a robust Microsoft C and C++ implementation of getopt, which led to devoid results.

This software is a modification of the Free Software Foundation, Inc. getopt library for parsing command line arguments and its purpose is to provide a Microsoft Visual C friendly derivative. The source code provides functionality for both Unicode and Multibyte builds and supports getopt, getopt_long, and getopt_long_only. Additionally the code supports the POSIXLY_CORRECT environment flag. The library uses the _UNICODE preprocessor directive when defining the getopt, getopt_long, and getopt_long_only functions. These functions are mapped back to getopt_a\getopt_w, getopt_long_a\getopt_long_w, and getopt_long_only_a\getopt_long_only_w respectively. This improvement was made to allow a single DLL to be used in both multibyte and Unicode projects.

The original GNU code used several header and implementation files containing numerous preprocessor directives specific to Linux environments which have been removed. After removing unneeded dependencies, it was condensed into a single header and implementation file which can be compiled into a DLL, LIB, or directly included into any Visual C, C++, or MFC project. The getopt library can be used by proprietary software, however; certain measures need to be taken to ensure proprietary code adheres to the Lesser GNU Public License for non-derivative works. Please refer to the licensing section of this article for more details on how this software is licensed.

For the sake of brevity, this article doesn't discuss how to use the getopt functions. Anyone new to using the getopt functions should refer to the GNU tutorial for using getopt.

Licensing

Since getopt is licensed under LGPL, it is free to use in proprietary software under some restrictions. When using this library as part of a proprietary software solution, it is important that the library is used as a dynamically linked library (DLL) and is not statically linked or directly compiled into proprietary source code. Static linkage requires that your software be released GPL. Therefore, by keeping the library separately referenced via Dynamic Link Library (DLL), allows the DLL to be modified and updated without changing the proprietary software which utilizes the library; under this condition proprietary software is said to "use" the library. Thus, it is not considered to be a derivative work and can be distributed freely under any license.

Preprocessor Definitions

Compiling getopt as a Dynamic Link Library (DLL) requires the preprocessor definition of EXPORTS_GETOPT. The definition of EXPORTS_GETOPT sets the internal preprocessor definition _GETOPT_API to the value __declspec(dllexport). Compiling getopt as a Static Library (LIB) or directly including the source and header file within a project requires the preprocessor definition of STATIC_GETOPT. The definition of STATIC_GETOPT clears the value of the internal preprocessor definition of _GETOPT_API. Compiling software to use getopt.dll requires that no library specific preprocessor definitions be used. When no library specific preprocessor definitions are used the value assigned to the internal preprocessor definition _GETOPT_API is __declspec(dllimport).

The following code segment located in getopt.h is responsible for mapping the correct version of the getopt, getopt_long, and getopt_long_only functions. The getopt functions appended with _a to denote ANSI characters using the char type and Unicode functions are appended with _w to denote wide characters using the wchar_t type.

Using this Code with C++ Precompiled Headers

When using this code statically within a C++ project with precompiled headers, it is necessary to rename getopt.c to getopt.cpp in order to circumvent the following compiler error:

"C1853 - Precompiled header file is from a previous version of the compiler,
or the precompiled header is C++ and you are using it from C (or vice versa)."

Additionally precompiled header file must be added as the first include of the getopt.c or getopt.cpp file. For example, if you are using "stdafx.h" as the precompiled header, the following would be expected:

Since you are new to developing in windows, I will give you a quick run through a direct compile vs. using a DLL or LIB. This will keep things simple.

1. Copy the .c and .h file directly to your project directory where your other .c and .h files are located.

2. From Visual Studio add the getopt.h and getopt.c to your project directly. If you are using MFC see the section of the article on using the files in MFC projects with precompiled headers.

3. Add the STATIC_GETOPT preprocessor definition to your project by doing the following:
1. From Visual Studio Solution Explorer, Right Click "Your Project Name" select properties.
2. Under The "Your Project Name" Properties Page, From the left hand tree control Click on C/C++ --> Preprocessor.
3. Edit the Preprocessor Definitions value located to the left of the tree control. Goto the end of line .. you should see something like WIN32;_DEBUG;... add STATIC_GETOPT to the end of that line. It should look something like this WIN32;_DEGUG;STATIC_GETOPT

4. Add #include "getopt.h" to the file you wish to perform command line parsing in.

I hope this helps, If not feel free to contact me again and I am willing to chat by phone to help you out. I could send you email's with screen shots if you need.

You should be able to place the files in your project folder without including them in your project. You can also add additional include paths to your project under the project properties. I would not advise placing the files into the default VC library path. The most important thing you need to do is define STATIC_GETOPT. I would advise reading a tutorial on creating and using DLLs in Visual C projects.