0

g++ can't find a certain function definition, even though it's clearly defined in a included library.


main.cpp

#include <StormLib.h>

int main() 
{
    HANDLE hMpq = NULL;
    SFileOpenArchive("file_name", 0, MPQ_OPEN_READ_ONLY, &hMpq);

    return 0;
}

StormLib.h

// ...
bool   WINAPI SFileOpenArchive(const TCHAR * szMpqName, DWORD dwPriority, DWORD dwFlags, HANDLE * phMpq);
// ...

Compiling

$ g++ main.cpp 
/usr/bin/ld: /tmp/ccNSvuaq.o: in function `main':
main.cpp:(.text+0x4b): undefined reference to `SFileOpenArchive'
collect2: error: ld returned 1 exit status

As you can see, SFileOpenArchive() is clearly defined in StormLib.h, and the header file is found by g++, but it still says SFileOpenArchive() is not defined.

This seems to be a commonly asked question on StackOverflow, and most solutions tell me to include the library in the compile command. This is what I've tried:

$ g++ main.cpp /usr/local/include/StormLib.h
$ g++ -o test main.cpp /usr/local/include/StormLib.h

Both commands give the same error as the first.

The most similar question with an answer is this one with over 1400 votes, but it does not solve my problem.

I'm new to C++ so this likely has a simple solution.

Mossmyr
  • 909
  • 2
  • 10
  • 26
  • 2
    It is not defined in the header, it is only declared. You need to link to the library or include the cpp file containing the body of the function into the list of files sent to the compiler. – RHertel Oct 28 '19 at 19:24
  • Ouch. StormLib.h was created by a `make install` command from StormLib's source code. Any idea on what I should do? Include the source code file in my compile command perhaps? – Mossmyr Oct 28 '19 at 19:28
  • 2
    You're not including the library; you're including a header file. The library will be a .lib, .so, .a, dependent on what, and how, you're consuming it. The header is just for compilation, the meat is in the lib itself. If the library is already in your lib path, i'm hesitant to suggest `g++ main.cpp -lstorm` *may* work. Otherwise you'll have to drive the full path-to-lib in your link line. – WhozCraig Oct 28 '19 at 19:31
  • @WhozCraig Wow `-lstorm` that did the trick. When running `make install` I mentioned in an earlier comment a file `/usr/local/lib/libstorm.a` was also created, and I guess that's the library. You can post it as a solution and I will accept it. – Mossmyr Oct 28 '19 at 19:35
  • Did you link to the library (.lib or .so)? –  Oct 28 '19 at 20:09

0 Answers0