using CMake Part II – Build MFC applications

It’s hard to google an article about building drivers in CMake, the same thing happens to MFC. There are many articles about CMake on building MFC applications, however, they use MFC flag to build. It’s supported only by VC IDE generators.

# set MFC attributes
set(CMAKE_MFC_FLAG 1) # 2 if use MFC as dll
ADD_DEFINITIONS(-D_AFXDLL) # if use MFC as dll

We must dig to know how MFC applications are built, so that we can use generators like NMake, Jom.

OK, here it is:

# set MFC attributes
#set(CMAKE_MFC_FLAG 1) # 2 if use MFC as dll
#ADD_DEFINITIONS(-D_AFXDLL) # if use MFC as dll

function (set_mfc_options _target)
    FIND_FILE(MFCPATH “afxwin.h”)
    IF(NOT (“${MFCPATH}” STREQUAL “MFCPATH-NOTFOUND”))
        GET_FILENAME_COMPONENT(MFC_INC ${MFCPATH} DIRECTORY)
        INCLUDE_DIRECTORIES(${MFC_INC})
    ENDIF()

    FIND_LIBRARY(MFC_LIBPATH “uafxcw$<$<CONFIG:Debug>:d>”)
    IF(NOT (“${MFC_LIBPATH}” STREQUAL “MFC_LIBPATH-NOTFOUND”))
        GET_FILENAME_COMPONENT(MFC_LIB ${MFC_LIBPATH} DIRECTORY)
        LINK_DIRECTORIES(${MFC_LIB})
    ENDIF()

    TARGET_LINK_LIBRARIES(${_target} uafxcw$<$<CONFIG:Debug>:d> Uxtheme windowscodecs)
    SET_TARGET_PROPERTIES(${_target} PROPERTIES LINK_FLAGS “/ENTRY:wWinMainCRTStartup”)
endfunction()

First of all, we need to ensure MFC headers and libraries are in the search path list. They should be there if you did full installation, however, they may not in the list if you use WDK/DDK directly.

Second, add mfc libraries to projects, so that linkers can find all used functions, classes.

Third, we need to add several libraries from WDK, we needs uxtheme, windowscodecs. This is required only when you use MFC as static library.

Finally, we need to set the application entry point to wWinMainCRTStartup. Unlike to other Win32 applications, MFC applications don’t provide WinMain function as the entry point, instead, they implement a singleton object, theApp.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s