I've got some trouble linking to a library I've compiled. Specifically, trying to create a program to test the wiimote-api: http://code.google.com/p/wiimote-api/
I compiled it myself in Dev-C++, exactly as the source on the site (with the linker option "-lhid"). I then moved the output, libwiimote-api.a to C:\MinGW\lib and wiimote-api.h to C:\MinGW\include
This is the top and main function of the testing program (FindHIDs is a function I got from the original author, which doesn't seem to have any problems):
int main(int argc, char *argv[])
{
cout<<"Wiimote controller test .01"<<endl<<"By David Johnson - 4/25/08"<<endl;
hidarray = FindHIDs(&num_hids);
int index;
cout << "Please choose the device that is your wiimote: " << flush;
cin >> index;
index--;
cout << "Connecting to "<< hidarray[index].Name <<endl;
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
mingw32-make -f "C:\Dev-Cpp\Makefile.win" all
g++.exe wiimote-tester.o -o "Project1b.exe" -L"C:/WINDDK/3790.1830/lib" -lwiimote-api -lhid -lsetupapi
wiimote-tester.o:wiimote-tester.cpp:(.text+0x544): undefined reference to `WiiM_ConnectWiimote(unsigned char*)'
mingw32-make: *** [Project1b.exe] Error 1
Execution terminated
I've checked every mistake I can think of, including double-checking that I'm spelling the function name correctly about 20 times. I'm very inexperienced with C++, particularly since guides and "how-tos" about programming in C++ are always about writing a program in one file and never have any information on linking or using precompiled libraries. So please, if you see an error in my code or if you know any common mistakes that I could me making, please tell me. As far as I can tell, this should work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, the conclusion must be therefore that while the header file contains a declaration of WiiM_ConnectWiimote(unsigned char*), the library does not.
I took a look at the source code and note that it is C not C++ and that the header is not written to support linkage with C++ code. So it will be looking for the C++ mangled name.
The simple fix is to modify your code thus:
extern "C"
{
#include <wiimote-api.h>
}
A more reusable solution is to modify the header itself this:
ifdef __cplusplus
extern "C" {
endif
// original header file content here
ifdef __cplusplus
}
endif
The header file will then work correctly for C and C++ code. You might want to post this fix back to the original author (who frankly should have known better).
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yeah, that... that was already specified in the compiler options, just like C:\MinGW\include was.
I've made sure that if it couldn't find the library, it would have thrown an error as such. But then, I can't find anything to explain why it isn't working, so...
Still, though, I added that line to be sure, and still the same undefined reference error.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>> [...]that was already specified in the compiler options, [...]
Well it may have been but it is a linker option, not a compiler option. I made the inference about -LC:\MinGW\include because to have started to link, it had to compile. Your log does not show the compilation because this is not a Rebuild-All log and wiimote-tester.o was already up-to-date. A Rebuild-All is useful when posting a log since it contains more complete information about your build and settings. Maybe we'd have seen your -L option in the wrong place.
Your log clearly shows that you have not set the library path (otherwise I would not have mentioned it!).
I see what you're saying, but I meant "compiler options" as the window that appears when one goes to the Tools menu and selects Compiler Options - in this case, under the tab Directories > Libraries. You're right about the compile log, though, so I must have added that after posting my log (I've also moved the files around a bit).
Regardless, that's fixed, and I'm still getting the same linker error. After making sure the contents are the same as what I already posted above, here is the current log for Rebuild-All:
Compiler: Default compiler
Building Makefile: "C:\Users\Axel\Programming\Wiimote2\Makefile.win"
Executing make clean
rm -f wiimote-tester.o wiimote-tester.exe
I've got some trouble linking to a library I've compiled. Specifically, trying to create a program to test the wiimote-api: http://code.google.com/p/wiimote-api/
I compiled it myself in Dev-C++, exactly as the source on the site (with the linker option "-lhid"). I then moved the output, libwiimote-api.a to C:\MinGW\lib and wiimote-api.h to C:\MinGW\include
This is the top and main function of the testing program (FindHIDs is a function I got from the original author, which doesn't seem to have any problems):
include "wiimote-tester.h"
define wVendorID 0x057e;
define wProductID 0x0306;
typedef struct _HID_INFO
{
BYTE Name[64];
BYTE DevicePath[256];
} HID_INFO;
HID_INFO* hidarray;
int num_hids;
HID_INFO FindHIDs(int num)
{...}
int main(int argc, char *argv[])
{
cout<<"Wiimote controller test .01"<<endl<<"By David Johnson - 4/25/08"<<endl;
hidarray = FindHIDs(&num_hids);
int index;
cout << "Please choose the device that is your wiimote: " << flush;
cin >> index;
index--;
cout << "Connecting to "<< hidarray[index].Name <<endl;
}
And wiimote-tester.h:
#include <windows.h>
#include <setupapi.h>
#include <cstdlib>
#include <iostream>
#include "C:\MinGW\include\ddk\hidsdi.h"
#include <wiimote-api.h>
using namespace std;
And finally the compile log:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
mingw32-make -f "C:\Dev-Cpp\Makefile.win" all
g++.exe wiimote-tester.o -o "Project1b.exe" -L"C:/WINDDK/3790.1830/lib" -lwiimote-api -lhid -lsetupapi
wiimote-tester.o:wiimote-tester.cpp:(.text+0x544): undefined reference to `WiiM_ConnectWiimote(unsigned char*)'
mingw32-make: *** [Project1b.exe] Error 1
Execution terminated
I've checked every mistake I can think of, including double-checking that I'm spelling the function name correctly about 20 times. I'm very inexperienced with C++, particularly since guides and "how-tos" about programming in C++ are always about writing a program in one file and never have any information on linking or using precompiled libraries. So please, if you see an error in my code or if you know any common mistakes that I could me making, please tell me. As far as I can tell, this should work.
Ok, the conclusion must be therefore that while the header file contains a declaration of WiiM_ConnectWiimote(unsigned char*), the library does not.
I took a look at the source code and note that it is C not C++ and that the header is not written to support linkage with C++ code. So it will be looking for the C++ mangled name.
The simple fix is to modify your code thus:
extern "C"
{
#include <wiimote-api.h>
}
A more reusable solution is to modify the header itself this:
ifdef __cplusplus
extern "C" {
endif
// original header file content here
ifdef __cplusplus
}
endif
The header file will then work correctly for C and C++ code. You might want to post this fix back to the original author (who frankly should have known better).
Clifford
Oh yeah... and I forgot to mention, I'm using Dev-C++ Ver. 4.9.9.2 running on Vista Home Premium edition
You never told the linker where to look for the library (with a -L<path> option). You need:
-LC:\MinGW\lib
in the linker options.
Unrelated, but if you specified C:\MinGW\include to the compiler (which you must have done), the line:
include "C:\MinGW\include\ddk\hidsdi.h"
can be shortened to:
include "ddk\hidsdi.h"
Clifford
Yeah, that... that was already specified in the compiler options, just like C:\MinGW\include was.
I've made sure that if it couldn't find the library, it would have thrown an error as such. But then, I can't find anything to explain why it isn't working, so...
Still, though, I added that line to be sure, and still the same undefined reference error.
>> [...]that was already specified in the compiler options, [...]
Well it may have been but it is a linker option, not a compiler option. I made the inference about -LC:\MinGW\include because to have started to link, it had to compile. Your log does not show the compilation because this is not a Rebuild-All log and wiimote-tester.o was already up-to-date. A Rebuild-All is useful when posting a log since it contains more complete information about your build and settings. Maybe we'd have seen your -L option in the wrong place.
Your log clearly shows that you have not set the library path (otherwise I would not have mentioned it!).
This line:
>> g++.exe wiimote-tester.o -o "Project1b.exe" -L"C:/WINDDK/3790.1830/lib" -lwiimote-api -lhid -lsetupapi
is the linker invocation; there is no -LC:\MinGW\lib there.
Clifford
I see what you're saying, but I meant "compiler options" as the window that appears when one goes to the Tools menu and selects Compiler Options - in this case, under the tab Directories > Libraries. You're right about the compile log, though, so I must have added that after posting my log (I've also moved the files around a bit).
Regardless, that's fixed, and I'm still getting the same linker error. After making sure the contents are the same as what I already posted above, here is the current log for Rebuild-All:
Compiler: Default compiler
Building Makefile: "C:\Users\Axel\Programming\Wiimote2\Makefile.win"
Executing make clean
rm -f wiimote-tester.o wiimote-tester.exe
g++.exe -c wiimote-tester.cpp -o wiimote-tester.o -I"C:/MinGW/include" -ansi -traditional-cpp
g++.exe wiimote-tester.o -o "wiimote-tester.exe" -L"C:/MinGW/lib" -lsetupapi -lwiimote-api -lhid
wiimote-tester.o:wiimote-tester.cpp:(.text+0x544): undefined reference to `WiiM_ConnectWiimote(unsigned char*)'
mingw32-make: *** [wiimote-tester.exe] Error 1
Execution terminated