Clion uses the MFC library. The local main method wants to run the solution after failure

Recently, I am writing JNI, and I intend to use java to call C or C + + to operate the underlying API of the operating system. MFC programming is a very old technology. A large number of windows APIs are provided in the MFC library, so we are going to write JNI to call the underlying Windows API.

Because I am used to using the products of Jetbrains family and for the seamless connection of shortcut keys from Idea series, I chose Clion as the preparation of C.
Of course, you can also choose Visual Studio here.

If you use Clion to write C, you need Configure the compilation environment (just configure it according to this article)
If you don't want to bother, you can use visual studio to download the MFC library. Here you can add the option of visual studio, select the Environment to the directory of VS, and restart Clion to take effect

Suppose you encounter the following error

Errors encountered

Nmake: fatal error u1077: "D: \ program ~ 2 \ mib055 ~ 1 \ 2022 \ commun ~ 1 \ VC \ tools \ MSVC \ 1430 ~ 1.307 \ bin \ hostx86 \ x86 \ cl.exe": return code "0x2"
Nmake: fatal error u1077: "D: \ program files \ Microsoft Visual Studio \ 2022 \ community \ VC \ tools \ MSVC \ 14.30.30705 \ bin \ hostx86 \ nmake. Exe": return code "0x2"
Nmake: fatal error u1077: "D: \ program files \ Microsoft Visual Studio \ 2022 \ community \ VC \ tools \ MSVC \ 14.30.30705 \ bin \ hostx86 \ nmake. Exe": return code "0x2"
Nmake: fatal error u1077: "D: \ program files \ Microsoft Visual Studio \ 2022 \ community \ VC \ tools \ MSVC \ 14.30.30705 \ bin \ hostx86 \ nmake. Exe": return code "0x2"

resolvent

You need to add the following lines of code to support MFC. Note that (JHelloWorld is my project name, you need to change it to your own project name)

set(CMAKE_MFC_FLAG 2)
set_target_properties(JHelloWorld PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
target_compile_definitions(JHelloWorld PRIVATE
        -DWIN32
        -D_DEBUG
        -D_WINDOWS
        -D_VC80_UPGRADE=0x0600
        -D_UNICODE
        -DUNICODE
        -D_AFXDLL
        )

If you haven't set the following configuration before, you need to add it if you want to run

(JHelloWorld is my project name. You need to change it to your own project name. all_handle.cpp is the source file with the main method. If you want to run this source file, you need to add the path of the source file on this line. I put the root path here, so I directly the file name.)

add_executable(JHelloWorld all_handle.cpp)

It should be noted that this line needs to be added to the above configuration, otherwise an error will be reported directly after saving

Complete cmakelists Txt (Reference)

JHelloWorld is the project name, which can be replaced by itself.
all_handle.cpp is a source file with main, which can be replaced by itself

cmake_minimum_required(VERSION 3.17)
project(JHelloWorld)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_MFC_FLAG 2)

add_executable(JHelloWorld all_handle.cpp)

# If you want to generate a dll library, you can open the following comments. Note that JHelloWorld is my project name,
# top_yumbo_jni_JavaCallCpp.cpp is used by JNI
# add_executable(jni ${SOURCE_FILES})
#add_library(
#        JHelloWorld
#        SHARED
#        top_yumbo_jni_JavaCallCpp.cpp
#)
#target_link_libraries(JHelloWorld) # The compilation target is preceded by add lib, so the Lib previously added will be generated into a dll file here


set_target_properties(JHelloWorld PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
target_compile_definitions(JHelloWorld PRIVATE
        -DWIN32
        -D_DEBUG
        -D_WINDOWS
        -D_VC80_UPGRADE=0x0600
        -D_UNICODE
        -DUNICODE
        -D_AFXDLL
        )

After completing the above settings, you can run normally. If there is no run button, you can select from the settings


The following is an example of normal run debug

Keywords: MFC microsoft intellij-idea

Added by jomama on Tue, 01 Feb 2022 07:03:13 +0200