11

I have a Qt project and would like to use an external library that includes "afxstr.h". Problem is that whenever I compile after linking to the lib and including their header, I get an error:

#error afxstr.h can only be used in MFC projects.  Use atlstr.h

Of course, my project is not an MFC project and I can't use atlstr.h instead because it's not my library.

I'm looking for a quick solution!

I'm using VS2010.

The lib in question is the Interactive Brokers API.

David Menard
  • 2,261
  • 3
  • 43
  • 67
  • Is this an open source and freely available library? If so, got a link? – HostileFork says dont trust SE May 22 '12 at 20:36
  • Interactive brokers API. It is free, but not open sourced – David Menard May 22 '12 at 20:39
  • Did you try modifying the library's header files so they include `atlstr.h` instead of `afxstr.h`? These two header files might target the same implementation of `CString`. – Frédéric Hamidi May 22 '12 at 20:48
  • 1
    Well there is a define to use std::string instead of CString. Still would be fun to know the actual answer to how to add MFC support :) – David Menard May 22 '12 at 20:52
  • @DavidMenard Fun...for who? :) Use that #define, MFC is so yesterday. *(Or as they say, "nostalgia isn't what it used to be, and it never was." Microsoft didn't use MFC for its own apps and just foisted it on the world. You might as well ask about integrating with Borland OWL at this point...or while you're at it, the BGI! http://stackoverflow.com/questions/8281573/what-is-used-to-make-graphics-on-dos )* – HostileFork says dont trust SE May 22 '12 at 20:56
  • @HostileFork I find general knowledge fun XD It's not like it was MY choice to use MFC... + they have other sources for tools that require MFC to build. – David Menard May 22 '12 at 21:05
  • I'm all for general knowledge...just not banging one's head against the wall so it feels good when you stop. It's an unfortunate MFC `CString` dependency, & an unfortunate API documentation website based on IFRAME *(so you can't link to individual pages, tons of console errors in the browser)*...are you sure there isn't a more up-and-coming library for your purposes? :-/ Perhaps even an open source one? This one doesn't seem very futureproof. – HostileFork says dont trust SE May 22 '12 at 22:22
  • The creators of the library created an unnecessary dependency on MFC. They are the ones who must fix it. Since MFC and Qt are both GUI frameworks, I would expect them to be butting heads constantly making compatibility impossible. – Mark Ransom May 22 '12 at 22:32
  • I ended up beeing able to compile because their tools come with sources, so everytime they use somethign MFC-related (most of which was CException) I added a #ifndef to stop it from compiling. – David Menard May 23 '12 at 14:40
  • May be I'm little late. :) But your Qt Project is built using what `qmake/gcc` tool-chain or `nmake/cl` tool-chain? Because I guess if your Qt Project uses `nmake/cl` tool-chain, you can add MFC support. Or correct it if I'm wrong.. – Ammar May 25 '12 at 13:31
  • have no idea why mess QT with MFC, if there's dependencies I would strongly recommend remove those dependencies to MFC, QT is obviously better designed than MFC. – zinking Jun 06 '12 at 06:06

2 Answers2

4

The respective setting is Configuration Properties/ General, Use of MFC.

The compiler option implied from that is /D "_AFXDLL" when using MFC in a DLL. As for linker options, curiously the explicit linking of windows import libraries (such as kernel32.lib) get removed.

Visual Studio seems to find the respective libraries automatically. However, the "Use of MFC" option is stored with the project file, so I can't say how it would translates to a custom build script.

The first include must be

#include <afx.h>

and you cannot include windows.h before that. Typically, that's the first include in stdafx.h if you use precompiled headers. Other than that, other MFC headers can be included freely as needed.

I doubt that this is the end of the story, getting MFC to play with anything is painful, and sometimes it's easier to give up :) A quick google reveals that there are solutions, but they involve additional code and are rather old.

peterchen
  • 40,917
  • 20
  • 104
  • 186
1

well, you have already know this, just make it more clear:

.pro file add: DEFINES += IB_USE_STD_STRING

avoid use MFC CString

raidsan
  • 777
  • 1
  • 7
  • 11