0

First of all, I understand there are many topics on this function, but I did not find any about this particular problem, sorry if I am repeating...

I have been working on a program in C++ that works with printers and I need to get the list of printers in the system.

I am using the EnumPrinters API and I am getting a compile error I don't understand.

This is my code:

#include <iostream>
#include <windows.h>
#include <winspool.h>

using namespace std;
int main()
{
    PRINTER_INFO_5 pi;
    PBYTE buffer[99];
    DWORD bufferSize = 0;
    DWORD bufferNeeded = 0;
    DWORD Entries = 0;

    bool r;

    r = EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 5, NULL, bufferSize, &bufferNeeded, &Entries);

    if (!r)
    { cout << "No printer found" << endl; }
    else { cout << "Found printers" << endl; }
}

When I try to compile (codeBlocks typical installation w/ gcc), I get this error:

C:\Programação\C++\lab\main.cpp 18 undefined reference to 'EnumPrintersA@28'

I think this may be a linker problem, but I don't know how to solve it...

Thank you!

SOLVED!

After some help I found out that the problem was that I wasn't importing the correct library. I thought including the header would be enough.

I needed to follow these steps (using 'winspool' instead of 'gdi32').

By the way, adding 'winspool.lib' did not solve it. Use 'winspool' instead (no '.lib')

Community
  • 1
  • 1
K09P
  • 480
  • 4
  • 13
  • 2
    possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Aug 20 '15 at 14:06

1 Answers1

1

Your linker is missing a .lib-file. If you lookup EnumPrinters at the MSDN documentation, you will see which library you have to add (somewhere at the bottom of the page, right before the comments).

In this case it's Winspool.lib. For gcc, add the commandline option: -lwinspool.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133