0

I am writing a program that requires me to include psabi.h. However, if I simply do #include <Psabi.h> I get the following error:

*path*\cch351oc.o:sourcefile1.cpp:(.text+0x7f): undefined reference to 'EnumProcessModules@16' *path*\cch351oc.o:sourcefile1.cpp:(.text+0xd5): undefined reference to 'GetModuleFileNameExA@16'

In some solutions, I see the line #pragma comment(lib, "psabi.lib") but this does not seem to do anything when compiling with g++.

I have tried the following lines in compiling:

g++ -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib" -lpsabi.lib sourcefile1.cpp

g++ -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\psabi.lib" sourcefile1.cpp

and also taking away the quotations and replacing the spaces with underscore. These all return:

c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../../mingw32/bin/ld.exe: cannot find -lpsabi.lib
collect2.exe: error: ld returned 1 exit status

For such a small error this is getting quite boring! Any help would be greatly appreciated.

Cheers.

EDIT: while What is an undefined reference/unresolved external symbol error and how do I fix it? identifies the same problem I have (though not specifically with psabi), it does not solve my issue - it does not tell me what to actually write on the command line.

EDIT: I have changed the functions in sourcefile1.cpp from EnumProcessModules to k32EnumprocessModules and GetModuleFileNameEx to K32GetModuleFileNameEx. I have tried the command lines:

g++ -DSAPI_VERSION=2 sourcefile1.cpp
g++ -DSAPI_VERSION=2 -lkernel32 sourcefile1.cpp
g++ -DSAPI_VERSION=2 -lkernel32.lib sourcefile1.cpp
g++ -DSAPI_VERSION=2 -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib" -lkernel32.lib sourcefile1.cpp
g++ -DSAPI_VERSION=2 -L"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\Kernel32.lib" sourcefile1.cpp

but all of them give the following error:

sourcefile1.cpp: In function 'long int getBaseAddress(const char*)':
sourcefile1.cpp:52:86: error: 'K32EnumProcessModules' was not declared in this scope
  if (K32EnumProcessModules(processHandle, moduleList, sizeof(moduleList), &sizeNeeded))
                                                                                      ^
sourcefile1.cpp:66:87: error: 'K32GetModuleFileNameEx' was not declared in this scope
    if (K32GetModuleFileNameEx(processHandle, moduleList[i], modulePath, modulePathSize) != 0)
                                                                                       ^

Reverting back to the original function calls (EnumProcessModules and GetModuleFileNameEx instead of K32EnumProcessModules and K32GetModuleFileNameEx) gives the same errors as before.

FINAL EDIT: g++ sourcefile1.cpp -lpsapi does indeed work on other computers. Seems the problem was a combination of originally not dropping the ".lib" as MvG pointed out, but also some problem in the installation files of mingw somewhere.

Community
  • 1
  • 1
w1nter
  • 337
  • 4
  • 23
  • Was that library compiled using `gcc` rather than `g++`? What are the results when you inspected it with the `nm` tool? – πάντα ῥεῖ Aug 22 '16 at 18:39
  • I compiled using g++. Also I am on windows so do not have the nm tool, should I use dumpbin instead? Using it on sourcefile1.cpp gives invalid format file... cheers. – w1nter Aug 22 '16 at 19:03
  • oops my mistake, though it gives the same error - file format not recognized, should I be passing sourcefile1.cpp to nm or something else? The errors that show are both in psabi.h, which it does not seem to be finding. This is weird since it finds the functions called from windows.h just fine, which is in the same folder. – w1nter Aug 22 '16 at 19:08
  • You have mingw, so you have `nm` of course, `nm` expects object or library files as input. Also it doesn't look the culprit is that library you mentioned, but your own code using some Windows API functions. And last but not least **order matters**. With that little information given, it's actually not possible to diagnose your problem, besides giving you that dupe. – πάντα ῥεῖ Aug 22 '16 at 19:10
  • I've decided that in the long run it would be more useful to have a single question on these kinds of errors, so I left your question closed as a duplicate, but added some more details to [that answer](http://stackoverflow.com/a/12574400/1468366) to address what I believe to be your problem. To make it short, drop the `.lib` suffix from your library name, i.e. write only `-lpsabi`. If that doesn't work, you should at least get a more useful error message, and might open a new question on that error message unless you can work it out on your own. – MvG Aug 23 '16 at 09:34
  • Come to think of it, and [reading](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683198(v=vs.85).aspx) [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682631(v=vs.85).aspx), you might be able to use `-DPSAPI_VERSION=2` as a command line argument to use the in-kernel version of these libraries. Not sure whether you need to write `-lkernel32` but I assume that to be automatic, so probably not. I've now voted to reopen after all, since I think this specific case could benefit from having different answers discussing the different alternatives. – MvG Aug 23 '16 at 09:48

0 Answers0