| |
Support Note
Question
How do I compile a CANLIB program using gcc (cygwin or MinGW) in Windows?
Answer
You should be able to use the Microsoft version of the canlib32 import
library. Here's an example when compiling using gcc from cygwin:
c:\program files\kvaser\canlib\samples\candemo>which gcc
gcc is an external : C:\cygwin\bin\gcc.exe
c:\program files\kvaser\canlib\samples\candemo>gcc --version
gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
c:\program files\kvaser\canlib\samples\candemo>gcc -o
candump_cygwin -I..\..\inc candemo.cpp -L..\..\lib\ms -lcanlib32
candemo.cpp: In function `void InitDriver()':
candemo.cpp:158: warning: passing NULL used for non-pointer converting 4
of `canStatus canIoCtl(int, unsigned int, void*, unsigned int)'
candemo.cpp:166: warning: converting of negative value `-0x000000001' to
`unsigned int'
candemo.cpp:169: warning: converting of negative value `-0x000000002' to
`unsigned int'
candemo.cpp:172: warning: converting of negative value `-0x000000003' to
`unsigned int'
candemo.cpp:175: warning: converting of negative value `-0x000000004' to
`unsigned int'
candemo.cpp:178: warning: converting of negative value `-0x000000005' to
`unsigned int'
candemo.cpp:181: warning: converting of negative value `-0x000000006' to
`unsigned int'
candemo.cpp:184: warning: converting of negative value `-0x000000007' to
`unsigned int'
candemo.cpp:187: warning: converting of negative value `-0x000000004' to
`unsigned int'
candemo.cpp: In function `int main(int, char**)':
candemo.cpp:468: warning: passing NULL used for non-pointer converting 4
of `canStatus canIoCtl(int, unsigned int, void*, unsigned int)'
candemo.cpp:674: warning: converting to non-pointer type `int' from NULL
c:\program files\kvaser\canlib\samples\candemo>
Q: Why doesn't the following work?
c:\program files\kvaser\canlib\samples\simple> gcc –o
simple_cygwin –L../../lib/ms –lcanlib32 simple.o
simple.o:simple.c:(.text+0xd4): undefined reference to `_canGetErrorText@12'
This problem has to do with the way gcc handles arguments for the linker
(object files and libraries). Unlike other arguments, they are
order-dependent. Here's an excerpt from the gcc online manual: (http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options)
|-l |library
Search the library named library when linking. (The second
alternative with the library as a separate argument is only for
POSIX compliance and is not recommended.)
It makes a difference where in the command you write this
option;
the linker searches and processes libraries and object files in
the
order they are specified. Thus, `foo.o -lz bar.o' searches
library
`z' after file foo.o but before bar.o. If bar.o refers to
functions
in `z', those functions may not be loaded.
...
As you can see, you need to put the "-lcanlib32" after "simple.o"
for everything should work as you expect.
|