c++ - 尝试在 C++、MinGW 上获取当前进程内存大小时出错

标签 c++ mingw clion mingw-w64 psapi

当我尝试调用 GetProcessMemoryInfo 时出现错误: 对 `GetProcessMemoryInfo' 的 undefined reference

我见过这个问题:Undefined reference to getprocessmemoryinfo@12

但它并没有解决我的问题。

我想知道我的进程在 RAM 中的大小,为此我需要使用“GetProcessMemoryInfo”方法。

我的问题是当我这样做时链接断开了。

CmakeLists.txt:

project(maxpath)

set(dir ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${dir}/build")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static-libgcc -static-libstdc++ -static -m64 -lpsapi")

file( GLOB LIB_ALG algorithms/*.hpp )
file( GLOB LIB_DS datastructures/*.hpp )
file( GLOB LIB_LOG include/*.h)

set(GRID_GENERATOR
        ${LIB_ALG}
        ${LIB_DS}
        ${LIB_LOG}
        grid/generator.cpp
        grid/grid.hpp)

set(GRID_SOLVER
        ${LIB_ALG}
        ${LIB_DS}
        ${LIB_LOG}
        grid/main_grid.cpp
        grid/grid.hpp
        include/memory_helper.cpp include/memory_helper.hpp include/fnv.h)
add_executable(gridGenerator ${GRID_GENERATOR})
add_executable(gridSolver ${GRID_SOLVER})

你可以看到我使用了-lpsapi参数,

我得到的错误是:

[ 33%] Linking CXX executable "some path...\gridSolver.exe"
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0xf1): undefined reference to `GetProcessMemoryInfo'
CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj):memory_helper.cpp:(.text+0x131): undefined reference to `GetProcessMemoryInfo'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\gridSolver.dir/objects.a(memory_helper.cpp.obj): bad reloc address 0x0 in section `.pdata'
C:/PROGRA~1/MINGW-~1/X86_64~1.3-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [some path.../gridSolver.exe] Error 1
CMakeFiles\gridSolver.dir\build.make:121: recipe for target 'some path.../gridSolver.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/gridSolver.dir/all] Error 2
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/gridSolver.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/gridSolver.dir/rule] Error 2
CMakeFiles\Makefile2:115: recipe for target 'CMakeFiles/gridSolver.dir/rule' failed
mingw32-make.exe: *** [gridSolver] Error 2
makefile:130: recipe for target 'gridSolver' failed

我正在使用 CLion 和 mingw-w64\x86_64-4.8.3-posix-seh-rt_v3-rev2\mingw64

是否有其他方法(除了使用 psapi)?

最佳答案

那里的答案是正确的。您应该针对 psapi 进行链接:

FIND_LIBRARY (PSAPI Psapi)
TARGET_LINK_LIBRARIES(gridSolver ${PSAPI})
TARGET_LINK_LIBRARIES(gridGenerator ${PSAPI})

或者您可以手动将其添加到链接器标志 - 您在示例中将其添加到编译器标志。

关于c++ - 尝试在 C++、MinGW 上获取当前进程内存大小时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38613234/

相关文章:

c++ - istream 运算符重载 C++

c++ - 获取Qt中的所有窗口

c - 如何在 Mingw 中将 C 程序编译为 dll 以与 Inno 设置和回调一起使用

mingw - 与libwinpthread的静态链接

c++ - Ubuntu - 在 CLion 中调试以代码 127 退出

c - 将 3rd Party C 文件与 C Lion 一起使用,初学者的视角

c++ - CLion 在远程计算机上找不到 C/C++ 标准库

c++ - Arduino有平台宏吗?

c++ - dllimport 或 dllexport 用于使用基类 dllimport 进行类声明

python - SWIG 语法错误