1

Hello I wanted to get the memory usage of a program (so i can for example detect memory leaks), but no matter what I do compiler throws:

undefined reference to `GetProcessMemoryInfo' collect2.exe: error: ld returned 1 exit status

I compile the program using batch file:

set FLAGS=-Wall -Wextra -g3 -O -Werror
set LINK=-static-libgcc -lpsapi -lkernel32
set SRC=*.c src/*.c test/*c
set OUT_FILE=-o build/build.exe

gcc %FLAGS% %LINK% %SRC% %OUT_FILE%

My code (simplified):

#include <windows.h>
#include <psapi.h>
#include <stdio.h>

int some_func_name()
{
    PROCESS_MEMORY_COUNTERS pmc;
    if ( !GetProcessMemoryInfo(GetCurrentProcess(),&pmc,sizeof(pmc)) ) { return 0; }
    return 1;
}

Iv searched for similar problems but all of them are solved by including -lpsapi or -lkernel32 but in my case neither helps to solve this error. Would appreciate some help or explanation as to why my method doesn't work

Goh
  • 33
  • 3
  • 1
    Just for kicks, change the gcc line to this: `gcc %FLAGS% %SRC% %OUT_FILE% %LINK%` – Jason Dec 23 '22 at 16:54
  • This actually worked, thank you. I feel so stupid now. Why does linking at the end change the entire compilation process? – Goh Dec 23 '22 at 16:57
  • No reason to feel stupid. I've run into this myself, and been stumped longer than I like to admit. The gcc option's order matters. All I know is putting linking options last fixes it for me. [Here](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) is a more descriptive reason. – Jason Dec 23 '22 at 16:59

0 Answers0