Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,

I'm trying to write a code in C++ to fetch users from local administrators group. While compiling I get a linker error saying
D:\Reference\CCPP\Get Local Admin Users\main.o	main.c:(.text+0x68): undefined reference to `NetLocalGroupGetMembers'

lmaccess.h file do have a declaration for this function. Not getting where it is going wrong.

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

int main(int argc, char **argv)
	LPCWSTR servername = NULL;
	LPCWSTR *localgroupname = (LPCWSTR *)"Administrators";
	LPBYTE *bufptr;
	LPDWORD entriesread;
	LPDWORD totalentries;
	RET = NetLocalGroupGetMembers(servername, *localgroupname, LEVEL, bufptr, MAX_PREFERRED_LENGTH, entriesread, totalentries, 0);
	return 0;

What I have tried:

Verified that lmaccess.h has the declaration for the function "NetLocalGroupGetMembers".
Updated 31-May-16 2:34am

1 solution

See the MSDN for the used function at NetLocalGroupGetMembers function (Windows)[^]. It states that the function is located in the Netapi32.dll. So you have to link your application against that DLL. The simplest solution is using a pragma statement in one of your source files (see also comment (C/C++)[^]):
#pragma comment(lib, "Netapi32")

Alternatively add it to your project using the project settings.
Share this answer
Darshan Parab 31-May-16 8:50am    
Hi Jochen,

Do I need to add pragma statement in header file or my code itself?
I tried adding it at the beginning of the code. It doesn't help.

Darshan G. Parab
Jochen Arndt 31-May-16 8:59am    
Using that pragma statement will only work with the Microsoft compiler (VisualStudio). I just noticed that you are probably using GCC or MinGW (indicated by the error message).

Then you have to specify the library using the -l<libname> option (append -lNetapi32 to your compiler command). It may be also necessary to specify the library path with the -L option (the path to your Windows SDK library folder).
Darshan Parab 1-Jun-16 6:36am    
Hi Jochen,

I used -lNetapi32 option while compiling and that solved the issue. Thanks for your guidance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900