wWinmain, Unicode, and Mingw - c++

I am working on learning the windows API and am using mingw as my compiler with Code::Blocks as my IDE. I have run into an issue with using the wWinMain function. I used the program located here link text. It compiles fine on VSC++ 2008 express but when using mingw i get the "undefined reference to WinMain#16" error. I have figured out what the problem is (i think). By replacing the wWinMain with just Winmain and the String pointer PWSTR with LPSTR it compiles perfectly. My question is, how can i fix this, and if not, is not using Unicode that big of a deal.
Thanks.

For old versions of MinGW, you can use a wrapper:
mingw-unicode-main:
https://github.com/coderforlife/mingw-unicode-main/
Simple wrappers to add wmain and wWinMain support in MinGW
These wrappers allow for use of wmain / wWinMain in MinGW seamlessly
with Unicode (WCHAR), regular (CHAR), or the ability to choose
(TCHAR).
The instructions for using them are in the files. Also take a look at
other programs that use them.
For new versions of MinGW, you should use the -municode option, like it says in the mingw-unicode-main readme:
Note: This should no longer be used as MinGW now has a built-in
solution. Add -municode to the command line (and possibly extern "C"
to the wmain function).
The -municode option works with MinGW-w64. In 2012-07, when I tried MinGW, it did not have the -municode option.
Here is how to install MinGW-w64:
Target Win32:
Home > Toolchains targetting Win32 > Personal Builds > rubenvb > gcc-4.7-release:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/rubenvb/gcc-4.7-release/
On Windows, you want "i686-w64-mingw32-gcc-4.7.2-release-win32_rubenvb.7z".
Extract folder to the root of your drive.
Rename the "mingw32" folder to "MinGW-32".
Target Win64:
Home > Toolchains targetting Win64 > Personal Builds > rubenvb > gcc-4.7-release:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/rubenvb/gcc-4.7-release/
On Windows, you want "x86_64-w64-mingw32-gcc-4.7.2-release-win32_rubenvb.7z".
Extract folder to the root of your drive.
Rename the "mingw64" folder to "MinGW-64".
Unicode-related questions:
How do I use the wmain() entry point in Code::Blocks?
Compiling Windows program in Dev-C++ gives error
#define _UNICODE not working with MinGW + CodeBlocks

Use the plain (non unicode) WinMain function, and then get your unicode command line using GetCommandLineW. MinGW doesn't know about wWinMain.
You are probably going to find working on MinGW difficult; last time I used it it did not support most of the wchar_t components of the C++ standard library (i.e. std::wifstream, std::wstring, etc) which made interfacing with Unicode Windows bits difficult.
Do you have anything against MSVC?

I know that I should have commented instead of answering, but I don't have enough reputation.
I want to add that I had to change the links to exe files in [Settings > Compiler... > Toolchain Executables > Program Files] in order to get the version of Community to run.
Also my CodeBlocks from 2016 claimed that it was Unicode but the -municode option didn't work, only the MiniGW update to the version by Community worked.
If you want to use main instead of wmain again, you have to delete -municode option.

Related

Converting a Linux Library Project into a Library Project usable in windows

I am attempting to convert the ndn library project found at "https://github.com/named-data/ndn-cpp" into something that can be imported into several existing mfc/wpf/forms Visual Studio Projects (building it as .lib or .dll would work).
As a note, it appears to have previously been converted into c# for windows for a older build of ndn, but is no long supported and will not connect with the current ndn network.(https://github.com/named-data/ndn-dot-net)
I have looked into the using the WSL features that they have added to windows 10, and the Visual studio Linux Cross Platform projects, but these all seem to only be able to make .exes that will run in windows not a .lib or .dll that can be imported into another project.
I have also look into the shared items project but what I was able to find didn't seem like it would work for what I'm trying to do.
Lastly, I tried using cygwin. I was able to compile and generate the linux style libraries(.a) on my windows 10 machine, but when i attempted to generate windows style dlls off the .o files(gcc -shared -o mydll.dll mydll.o) I ran into a large number of reference errors that I was unable to resolve.
Does anyone have any recommendations on which of these methods I should be using or if I should be attempting some other method entirely?
Does anyone have any good references or examples of how to do this for someone with limited Linux experience?
Thanks
Ok. I've tried going about this several ways now, and here's what I've learned that might be useful to someone else trying to do this and also where I stand so far:
If you have a simple Linux dll that you have written it's possible to compile it as a Windows dll using MSYS2 or MINGW, instructions are here: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
If you have something a bit more complicated like the program I'm trying to convert above, there is no quick fix to convert from Linux to windows, however you still might be able to compile your program for windows using Visual Studio.
Here's how you do it:
Download vcpkg from https://github.com/microsoft/vcpkg This is a linux package manager for windows, it will allow you to download windows equivalents to many common Linux packages (for the above I had to download boost and sqlite3)
Create your own unistd.h, here's a link to that: Is there a replacement for unistd.h for Windows (Visual C)?
Get dirent.h for windows, here's a link to that: https://github.com/tronkko/dirent
replace instances of gmtime with _mkgmtime or redefine gmtime as _mkgmtime: timegm cross platform
This got me about 90% of the way there (and from about 13,000 compiler errors to about 30), The rest of the errors so far have been for calls where there is no easy linux to windows conversion and those sections of code have had to be re-written. (In the code above this would be the socket code for the tcp/udp connections in the tranport files, Linux and Windows handle it pretty differently, boost does have a good guide for how to use it's sockets in windows though: https://www.boost.org/doc/libs/1_60_0/more/getting_started/windows.html)
So that's it. Hopefully this helps someone else down the line. I'll be adding more to this answer as I discover new things.

MinGW-w64 can't resolve threadpoolapiset.h declared functions

When I try to build the following code using MinGW-w64 in Eclipse, I get the error "Function 'CreateThreadpool' could not be resolved"
The code:
#define WINVER 0x0603
#define _WIN32_WINNT 0x603
#include <threadpoolapiset.h>
int main(void)
{
CreateThreadpool(nullptr);
return 0;
}
Note: If I remove the defines for windows version, then I get 2 errors:
'CreateThreadpool' was not declared in this scope
Function 'CreateThreadpool' could not be resolved
This means that the defines are making posible for threadpoolapiset.h header to declare the windows API functions.
So back to the initial example, the problem here is that the linker can't find the library where this function is defined.
According to MSDN this function is defined in library kernel32.lib and DLL kernel32.dll.
I found that in MinGW-w64 directories the kernel32 library is found as libkernel32.a, so I tryed including paths and libraries in C/C++ Build > Settings | Tab 'Tool Settings' > GCC C++ Compiler > Includes
I tryed including the directory, the library file and both. Also I did this in MinGW C++ Linker > Libraries
Does any one know how can I make it work?
The threadpool APIs were only added to the linker files in MinGW 4.0. You will need to upgrade.
Windows
If you downloaded MinGW-w64 through MSYS2, then upgrading should be simple; just follow the instructions here.
If you installed the Win-Builds binary package, simply uninstall that, download the new one, and install it.
If you got your MinGW-w64 as part of an IDE, you'll need to consult the IDE vendors to see how to upgrade.
Linux
Unfortunately MinGW-w64 4.0 is newer than most distributions provide. If you use Ubuntu, 15.10 is just around the corner and that should have it. If not, then you will need to build from source. The process for building from source is a little convoluted; go to the MinGW-w64 IRC channel on OFTC and ask there :/ That's what I had to do, anyway. Good luck.
Note to David Hefferman and company
MinGW-w64 doesn't use the same SDK model Microsoft uses. What is available is based on the version of MinGW used, and new things are added frequently and available in subsequent releases. (In fact the reason to use MinGW-w64 over vanilla MinGW is that it has support for most of the APIs that were introduced starting with XP.)

Is there any difference between Qt's MinGW, Code::Blocks' MinGW and MinGW it self?

Qt and Code blocks download package have their MinGW compiler. And there's standalone MinGW itself. Is there any difference between them?
Can I use Qt's MinGW to build Code blocks project or vice versa?
Did I need another MinGW if i want to build C++ project without Qt Creator or Code Blocks?
Or, can I just use Qt's or Code Blocks' MinGW without standalone version of MinGW?
Both Qt Creator and CodeBlocks just bundle a version of MinGW (GCC) in their installations for convenience, so that users don't have to install it and configure it manually. In both cases, you can choose to download or install the IDE without installing MinGW (GCC), in which case, you need to configure the IDE to use whatever compiler you want it to use (which not only can be any standalone MinGW/GCC installation, but also any other compiler, including MSVC (Microsoft compiler), ICC (Intel's compiler), or Clang).
Here are instructions to configure a custom compiler for Qt Creator. And here are instructions for CodeBlocks.
Is there any difference between them?
As far as I know, there is no significant difference between them, except for the version, of course. IDEs that are bundled with MinGW-GCC will typically come with one particular (and often a bit older / more stable) version of it. When you install a standalone MinGW-GCC, it can be whatever version you choose. There are differences between versions, but usually not anything that would "break" code, just faster / better compilations or additional advanced features (which are not used by "default" projects in any case).
Can I use Qt's MinGW to build Code blocks project or vice versa?
I'm pretty sure you can. I don't see any reason why not. Of course, you have to get the configuration right, as per the instructions I linked to above.
Did I need another MinGW if i want to build C++ project without Qt Creator or Code Blocks?
If you want to build things outside of an IDE that is configured with a particular compiler or installation of MinGW, then you will have to make sure to setup a few things. This process is easier when doing a standalone installation of MinGW, but you can also figure out how to take an existing MinGW installation (that came from Qt / CodeBlocks) and make it work under CMD / PowerShell / MSYS, but it's a bit trickier and more unusual.

Do I really need Visual Studio

Do I really need Visual studio to build c/C++ application on Windows.
Is there any way to have makefiles and get the application built.
You can use any compiler that'll target Windows (for example, MinGW, Digital Mars, Comeau or others). All of them can be driven by the command line so you can use your preferred make utility (or other build utility).
That said, Visual Studio is pretty nice (even the free version). And don't forget that it installs the command line compiler tools, so you can drive it from a makefile as well (unfortunately, it won't spit out a makefile for you anymore, but it has it's own command line tools to drive a build based on project settings if you want to jump from IDE to command line builds).
If you want to stick to the Microsoft toolchain but don't want the IDE, you can use cl and link to build from the command line in conjuction with either the MSBUILD system or NMAKE.
If you don't have the compiler it is available free with VC++ Express.
To install the Microsoft compiler without installing an IDE download the Windows SDK which is available for free.
http://en.wikipedia.org/wiki/Windows_SDK
Qt is very nice, and downloading and installing the SDK gets you the QtCreator which provides a consistent IDE across platforms, with very nice builtin help, build system, access to revision control, debugging, ... and it even includes the MinGW compiler and toolchain for you.
MinGW is something simpler: the usual GNU toolchain but setup such that it provides native Windows binary.
Last but not least, there is Cygwin which gives you the most Unixy flavour.
No, you don't.
I know you said c, but you can also check out MonoDevelop, should you choose to do more than just that: http://monodevelop.com/Download
No you do not. There are many free IDE's out there, or you can do everything from the command line with a non-microsoft compiler.
www.bloodshed.net offers a pretty good IDE that uses the Mingw compiler.
You can even do without extra tools as long as you have the compilers on your machine. Take a look here for an explainatin:
http://msdn.microsoft.com/en-us/library/ms235639.aspx
Not at all. It is quite possible to do Win32 programing, including OS and GUI programming, with gcc from Mingw.
For all my hobby work, if I can I use the gcc compiler from Mingw (for C or Ada), with Emacs as my IDE and gnumake for my build system. There are good Mingw ports for all the major revision control systems too, including Git.
That's the toolset I used to create the SETI#Home Service, which was a wrapper for the SETI#Home client that installed and ran it as a Windows service, did failure detection and auto restarts, and had a built in web server for monitoring. All that with no VisualStudio.
You can use Eclipse with C/C++ Development Toolkit and with Mingw Compilers. the only problem is that you will not have M$ Stuff like MFC, but you can use QT or wxWindows
Two solutions that immediately come to mind:
Cygwin
MinGW
Using makefiles does not prevent you from using the Microsoft C++ compiler, which is nice if you want to use the VS debugger.

Best way to setup a Windows build environment for C/C++

Basically I want to compile C/C++ using the GCC on Windows. The two competing platforms, as i see it, are MinGW and Cygwin. Each have their own benifits and limitations. MinGW compiles for Windows, whereas Cygwin needs the cygwin .dll. However installing libraries on MinGW is difficult, whereas on cygwin it's easier, using the setup.exe as a 'package manager'.
My question is what's the best way to compile on Windows. Would it be better to use MinGW and struggle with libraries (in which case is there an easy way of installing libraries on MinGW). Or should I use Cygwin, if so how do I get it to compile without needing the cygwin .dll, effectively compile for normal Windows.
The easiest and best way to compile on windows is to use visual studio express. It is free. A good reason for using cygwin is for cross platform builds. A good reason to use gcc is it supports some compiler candy the Microsoft compiler doesn't.
But all in all VC++ Express is the way to go for windows only development.
There is always the option of using -mno-cygwin with Cygwin to compile against Windows libraries like MinGW does:
gcc -mno-cygwin file.c -o test
It's not an easy choice and mainly depends on how much you will rely on other libraries. Cygwin really adds an additional layer, which is heavier but "emulates" the Linux environment better than MinGW.
Edit:
Qt Creator allows you to compile with MinGW on Windows (and gcc on Linux, ...), has an IDE that makes debugging more friendly than gdb. If you need libraries, you still have the option of using the Qt libraries. That would be a good alternate solution to the Visual Studio if you really want to stick to gcc for future portability.
You might want to look at CodeBlocks. It is generally used to build WxWidgets apps, but it wraps MinGW nicely.
Actually, there's option #3: if your edition of Windows permits it, you can install Microsoft Services For Unix / Subsystem for Unix Applications, and then get gcc from SUACommunity. It has a package manager, too. Of those 3 options, this will give you behavior closest to a true Unix system.
However, the resulting applications aren't Win32 applications; they're SUA applications, and will require SUA to run. If you write code for yourself, it's usually not a problem if you write code for yourself, but if you want to write and distribute a proper Windows application, I would suggest staying away from anything that tries to emulate Unix, so MinGW it is.
Qt Creator comes with MinGW as standard and can be used to build projects that don't actually use the Qt framework.
There's an easy to install MinGW wrapped GCC at equation.com.
Click "Programming Tools" then "Fortran, C, C++", download the release you prefer, install and use.
One possible method is to use CMake which can build Visual Studio project from your sources and then compile from Visual Studio. It can build project for other IDEs too, so you can go cross platform.
I think the answer depends on whether you intend to use libraries or compile programs that are targeted to POSIX or a POSIX-based target. That's what Cygwin is intended for, while MinGW is more intended for compiling Windows-targeted programs using GCC.
Another option is TDM MinGW: http://www.tdragon.net/recentgcc/
It's basically an unofficial fork of MinGW with the latest GCC compiler available for both 32- and 64bit windows machines. Even some MinGW developers themselves use it.
Equation Gcc is even better: http://www.equation.com/servlet/equation.cmd?fa=fortran