2

I am working with code blocks using MinGW, and I have a coding project I've been working on for some time. The project I've been using has been console application, and the debug functions I have rely on printing to the console.

However, recently, I've tried to implement windows 'GetOpenFileName()' function to pull up an open file box so it'd be a lot easier for users to open files with. However, when using a project in console application with the headers included, the MinGW compiler acts like it can't find the function. I found however changing the project to win32 GUI allows the compiler to find the function (I don't entirely understand why but that's just me).

The problem is thus: I tried to output debug information to console, but of course now the application is win32 GUI, the console doesn't display and thus I cannot see the debug information, and I can't switch it back to console application or the windows open file box code won't compile. How do I both display the console and still allow the project to correctly compile the windows GUI code?

I cannot alter the debug functions or use different ones because they are written into all of the classes to assist with back-tracing errors and there are at least 43 files, most of which use the debug functionality.

6
  • 1
    Including <windows.h> in a console application should still work. If you absolutely need a console and can't have one, though, there's AllocConsole. Commented Aug 27, 2012 at 20:06
  • I did include <windows.h> "when using a project in console application with the headers included" - it doesn't compile unless, for some odd reason, the project is set to win32 GUI. I'm figuring it's a code blocks thing. How do I use 'AllocConsole'? Will it automatically display printf's etc? If this is an answer you should post it for points. Commented Aug 27, 2012 at 20:09
  • For the record, I did try to inquire on the code blocks forums about this but... they really don't like answering questions. Commented Aug 27, 2012 at 20:10
  • Could you post a small sample of something that doesn't work and the errors you get? I've been using both the Windows API, and GUIs within with C::B for a long time now without any trouble. Commented Aug 27, 2012 at 20:11
  • 2
    Is it by chance saying Undefined reference to GetOpenFileNameA@4? That's a linker error, and can be solved by linking to comdlg32.lib. Commented Aug 27, 2012 at 20:14

2 Answers 2

3

And so the problem appears not to be caused by a project type, but a setting/compiler option. The compiler can find the function declaration just fine, but the linker now complains because it can't find the definition.

In order to remedy this, you need to link to the correct library (which, as stated in the MSDN documentation, is comdlg32.lib).


In CodeBlocks, you can do this by:

  1. Go to the Project menu and click Build Options.
  2. Click the Linker Settings tab.
  3. Click Add.
  4. Type comdlg32 and press OK.
  5. Click OK to save your change and exit.

Your project will now link to that library when building. If you want all programs, project or not, to link to that library, you can replace step 1 with the following step instead:

  1. Go to the Settings menu and click Compiler and Debugger.

The rest is the same from there, but on a global basis.


For what it's worth, if you're using raw command line building, you can add the -lcomdlg32 option to produce the same effect.

Sign up to request clarification or add additional context in comments.

Comments

2

Use AllocConsole to create a console within a GUI app.

1 Comment

The thing is it doesn't actually look like the OP has a GUI, but is just calling a winapi function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.