C + + / Fortran calling. Exe executable file under Windows

Time:2021-9-13

software environment

#Operating system = windows10
#Compile chain tool= 
#	gcc, g++, GNU Fortran (MinGW.org GCC Build-2) 9.2.0
#	GNU Make 3.82.90	Built for i686-pc-mingw32
#	GNU ld (GNU Binutils) 2.32
#	CMake version 3.18.1

The MinGW compiler tool is installed on this machine, and\MinGW\binAdd to environment variable.

Cmake compilation configuration under Windows

Set the generator for the project

It is found in the process of using cmake locally that it is used by defaultNMake MakefilesAs a generator, the configuration failed because it is not installed.

You want to set the generator to installedMinGW Makefiles

Command Line

cmake .. -G "MinGW Makefiles"

CMake GUI

the first timeConfigureSpecifies the generator for the project when.

ChooseMinGW Makefiles, use the default compiler.

If the generator has been specified, it needs to be modified. Can clearCMake CacheAfter againConfigureFile->Delete Cache

PreLoad.cmake

The cmake generator is set before processing cmakelists.txt, so it cannot be set in cmakelists.txtCMAKE_GENERATORTo modify the default generator.

You can add in the project root directoryPreLoad.cmakeSuch a file can be used to modify the configuration in advance, as follows.

set(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE)

Set make

Even if you have added environment variables to MinGW, you cannot use make directly on the command line. GCC, G + +, gfortran can be used directly.

Because in\MinGW\binUnder, the name of the executable corresponding to make ismingw32-make, copy a copy in the same directory and rename it to make.

Sample program

CMake

The following is the content of cmakelists.txt file,

# name: exe_test
# date: 2021/3/14
#

cmake_minimum_required(VERSION 3.3)
project(exe_test CXX Fortran)

add_executable(hello source/hello.cpp)
add_executable(caller_cpp source/caller.cpp)
add_executable(caller_fort source/caller.f90)

A small problem in setting up FORTRAN language

-->$ cmake ..
-- The CXX compiler identification is GNU 9.3.0
CMake Error: Could not find cmake module file: CMakeDetermineFORTRANCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_FORTRAN_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_FORTRAN_COMPILER
...

The reason for this problem isCMakeLists.txtIs case sensitive,FortranOnly initial capital letters should be used.

# project(exe_test CXX FORTRAN)
project(exe_test CXX Fortran)

source/hello.cpp

// name: hello.cpp
// date: 2021/3/14
//

#include 
using namespace std;

int main()
{
        cout << "Aloha!" << endl;

        return 0;
}

source/caller.cpp

// name: caller.cpp
// date: 2021/3/14
//

#include 
#include 
#include 
#include 
#include 

using namespace std;

void Launch(LPCTSTR lpApplicationName)
{
        // additional information
        STARTUPINFO si;
        PROCESS_INFORMATION pi;

        // set the size of the structures
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );

        // start the program up
        CreateProcess( lpApplicationName,   // the path
                        (char*)"",        // Command line
                        NULL,           // Process handle not inheritable
                        NULL,           // Thread handle not inheritable
                        FALSE,          // Set handle inheritance to FALSE
                        0,              // No creation flags
                        NULL,           // Use parent's environment block
                        NULL,           // Use parent's starting directory
                        &si,            // Pointer to STARTUPINFO structure
                        &pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
        );

        cout << "process id: " << pi.dwProcessId << ", thread id: " << pi.dwThreadId << endl;

        // Close process and thread handles.
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
}

int main(int argv, char* args[])
{
        cout << "caller_cpp:" << endl;

        // 1.
        //You can add a path. Use \ \ instead of \ \ in the path/
//      WinExec("hello.exe", SW_SHOW);

        // 2.
//      SHELLEXECUTEINFO shell = { sizeof(shell) };
//      shell.fMask = SEE_MASK_FLAG_DDEWAIT;
//      shell.lpVerb = "open";
//      shell.lpFile = "hello.exe";
//      shell.nShow = SW_SHOWNORMAL;
//      BOOL ret = ShellExecuteEx(&shell);

    	// 3.
        // ShellExecuteA
//      ShellExecute(NULL, "open", "hello.exe", NULL, NULL, SW_SHOWDEFAULT);

        // 4.
//      system("hello.exe");

        // 5.
        Launch("hello.exe");

        // 6.
//      _execv("hello.exe", args);

        return 0;
}

Collected a variety of ways, it is recommended to useCreateProcess(), please refer to Microsoft docs for details.

source/caller.f90

! name: caller.f90
! date: 2021/3/14
!

PROGRAM Caller_fort

        PRINT *, "Caller_fort"

        ! Fortran 2008
        call execute_command_line ("hello.exe", wait=.false.)

        !
        call system("hello.exe")

END

execute_command_lineyesFortran2008Standard start supported functions.

reference material

  1. cmake_ Selecting a generator within CMakeLists.txt – Stack Overflow
  2. Using cmake with fortran – Stack Overflow
  3. windows C_ C + + opens, closes and monitors other exe programs in one program_ lumanman_ Blog – CSDN blog
  4. C + + method to open EXE file (VS2008) – work hard work smart – blog Garden
  5. Open EXE file in C + +_ Nanmu big brother’s blog – CSDN blog
  6. Calling exe executable file in C++ program Accumulate a little bit and keep yourself – CSDN blog_ C + + call EXE file
  7. ShellExecuteA function (shellapi.h) – Win32 apps _ Microsoft Docs
  8. Creating Processes – Win32 apps _ Microsoft Docs
  9. _exec, _wexec Functions _ Microsoft Docs
  10. EXECUTE_COMMAND_LINE (The GNU Fortran Compiler)
  11. SYSTEM (The GNU Fortran Compiler)