That's correct not related to <string.h>. It's not needed at all in the example to get the link error. I have attached the compiler log. This is using version 4.9.9.2 with the default installation.
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Examples\Makefile.win"
Executing make clean
rm -f main.o Project1.exe
Wayne,
Thanks but I don't think that's it. If comment out WNetAddConnection3 from my source (or WNetConnection2 from the sample in the orginal post) then compile and link complete without any errors. I have checked WINNETWK.H and it is defined there. I have also searched the forum/FAQ/MINGW/WWW without any luck.
Bert
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"I have checked WINNETWK.H and it is defined there."
The header is only half, or less of the puzzle. A header is just a list of function prototypes. There is not real, working code in a header. Its in the library. That is why you are getting a link error. You included the promises (the header), but not the code (the library).
I strongly encourage you to read the section in the thread in this forum titled "FAQ - Please Read Before Posting" on the compile log, including headers and linking libraries. It talks inmore detail about this.
There are a lot of people who really don't get this - and ask questions like "What header does so and so" - a header really does nothing.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Does anyone know why WNetAddConnection2 causes a linker error "undefined reference" during compile? For example:
/ test /
include <windows.h>
include <winnetwk.h>
include <string.h>
include <iostream>
include <cstdlib>
using namespace std;
int main(void)
{
NETRESOURCE NetResource;
char Password[30] = "letmein";
char UserName[30] = "user";
int res;
NetResource.dwScope = RESOURCE_CONNECTED;
NetResource.dwType = RESOURCETYPE_DISK;
NetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
NetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
NetResource.lpLocalName = "T:";
NetResource.lpRemoteName = "\server1\share";
NetResource.lpComment = NULL;
NetResource.lpProvider = NULL;
res = WNetAddConnection2(&NetResource, Password, UserName, CONNECT_INTERACTIVE);
}
Yes, I understand. Thanks.
Bert
Probably not related but by <string.h> do you mean <string> (C++ strings) or <cstring> (C strings)? Can we see a compile log from that code as well?
That's correct not related to <string.h>. It's not needed at all in the example to get the link error. I have attached the compiler log. This is using version 4.9.9.2 with the default installation.
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Examples\Makefile.win"
Executing make clean
rm -f main.o Project1.exe
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
main.o:main.cpp:(.text+0x1bf): undefined reference to `WNetAddConnection2A@16'
collect2: ld returned 1 exit status
make.exe: *** [Project1.exe] Error 1
Execution terminated
Your compile log shows you did not successfully link any libraries. (This is probably the cause of your linker error).
Did you try to link anything?
Wayne
Wayne,
Thanks but I don't think that's it. If comment out WNetAddConnection3 from my source (or WNetConnection2 from the sample in the orginal post) then compile and link complete without any errors. I have checked WINNETWK.H and it is defined there. I have also searched the forum/FAQ/MINGW/WWW without any luck.
Bert
-lmpr
Well, thank you very much anonymous contributor. Problem resolved.
For those of you experiencing this/similar problems, just add -lmpr to your linker options.
Bert
Do you understand what that does?
It links a needed library.
Wayne
"I have checked WINNETWK.H and it is defined there."
The header is only half, or less of the puzzle. A header is just a list of function prototypes. There is not real, working code in a header. Its in the library. That is why you are getting a link error. You included the promises (the header), but not the code (the library).
I strongly encourage you to read the section in the thread in this forum titled "FAQ - Please Read Before Posting" on the compile log, including headers and linking libraries. It talks inmore detail about this.
There are a lot of people who really don't get this - and ask questions like "What header does so and so" - a header really does nothing.
Wayne