How to link static library using windows cmd

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings,

​I am using windows command prompt and avr-gcc to compile a c file for atmega644, in directory C:\Users\Yong\Desktop\display and with the command below: 

avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os testc11.c -o  testc11.elf

​I have a <liblcd.a> file in the same directory where <testc11.c> is stored. And I am having trouble figuring out where to modify the linker command namely where to add -llcd.
​I have been instructed to add -llcd to the end of the linker command, but I couldn't quite manage to pull it off.
​I have tried several ways to do it including:
1)  avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os testc11.c -o -L/C:/Users/Yong/Desktop/display -llcd testc11.elf
2)  avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os -L/C:/Users/Yong/Desktop/display -llcd testc11.c -o testc11.elf
3)  avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os -llcd testc11.c -o testc11.elf

but it kept saying "cannot find -llcd". Screenshot:

Any help on what to type into cmd will be very much appreciated. Thanks.
​Also I have searched through the internet for manuals, examples and documentation but they exacerbated the confusion since I am a true rookie and many things are still too heavy for me to comprehend. Any helpful advice on learning would be very much appreciated. Thanks in advance.

 

 

 

 

This topic has a solution.

Rookie

Last Edited: Tue. Dec 12, 2017 - 11:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not on a computer now, so this is an untested hypothesis :
.
Your command attempts are both compile-AND-link "in one go". This will not work when you want to link explicitly with a library.
.
In such a case you must emit two separate commands. The first to compile your source file to an object file. The second to link that object file and your library producing the elf file.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Dec 12, 2017 - 07:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Other hypothesis: Directory with library not in library search path. Try giving file spec in library option with full path from root.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

JohanEkdahl wrote:
Directory with library not in library search path. Try giving file spec in library option with full path from root.
That's my guess too. (your other one is wrong - you can compile and link, even with libs, in a single command).

 

The error is simply that it cannot find the lib. When -llcd is used it will be looking for liblcd.a so the question is whether that is in the "current directory". If not that -L should be used on the command line. His (1) was almost right:

avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os testc11.c -o -L/C:/Users/Yong/Desktop/display -llcd testc11.elf

but that appears to have one '/' too many. Surely it should just be:

avr-gcc -mmcu=atmega644p -DF_CPU=12000000 -Wall  -Os testc11.c -o -L C:/Users/Yong/Desktop/display -llcd testc11.elf

though personally I would not be giving an absolute path with C: in it but simply a relative path from where you are now to where the file is located.

 

What I do wonder though is why is anyone bothering with static links for AVR code anyway? It only makes sense if the author of the LCD code deliberately wanted to withhold source and distribute only binary. Otherwise you might as well just add the lcd.c as a compilation input and then use -ffunction-sections and -gc-sections to discard code that is not called (to get the same effect as multiple .o in a .a would have delivered)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
When -llcd is used it will be looking for liblcd.a  

Indeed.

 

Maybe the OP has just called it lcd.a ... ?

 

The GCC Linker Documentation wrote:

-llibrary
-l library

 

-llibrary

-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.

 

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with -L.

 

Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories.

 

https://gcc.gnu.org/onlinedocs/g...

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Re-reading OP I notice:

YJQ wrote:
​I have a file in the same directory
So it is liblcd.a but what's more, if it's in the same directory it should just be found anyway. Time for a quick experiment...

C:\SysGCC\avr\bin>type foo.c
int foo(int n) {
        return n + 1;
}

C:\SysGCC\avr\bin>type avr.c
#include <avr/io.h>

int foo(int);

int main(void) {
        PORTB = foo(7);
        while(1);
}

C:\SysGCC\avr\bin>avr-gcc -mmcu=atmega16 -Os avr.c foo.c -o avr.elf

C:\SysGCC\avr\bin>avr-gcc -mmcu=atmega16 -c foo.c -o foo.o

C:\SysGCC\avr\bin>avr-ar rcs libfoo.a foo.o

C:\SysGCC\avr\bin>avr-gcc -mmcu=atmega16 -Os avr.c -lfoo -o avr.elf
c:/sysgcc/avr/bin/../lib/gcc/avr/5.3.0/../../../../avr/bin/ld.exe: cannot find -lfoo
collect2.exe: error: ld returned 1 exit status

C:\SysGCC\avr\bin>avr-gcc -mmcu=atmega16 -Os avr.c -lfoo -L . -o avr.elf

Well that worked better than I hoped! Not only did I manage to use -lfoo but I even hit the same problem as OP here and then solved it!

 

So the trick is to add "-L ." because it seems that the default search for libs does NOT include the current directory.

So if liblcd.a really is in the same directory as the code being built just add "-L ." and it should be found.

 

Quod Erat Demonstrandum.

Last Edited: Tue. Dec 12, 2017 - 11:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

YJQ wrote:
I have a <liblcd.a> file

 

Hey YJQ! Please don't put angle brackets ( < > ) around filenames in posts here! (Unless, of-course it's a filename in an #include directive in a code block.)

 

It upsets the forum software somewhat, and quoting such text becomes awkward.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you guys for your precious insights.

Thanks clawson especially for the solution, it directly solved the problem that had been bugging me whole day.
​It turned out that I messed up where to place -llcd and -L . thanks man ! 

Rookie

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
So the trick is to add "-L ." 

 

GCC Directory Options Documentation wrote:

 

-Ldir

Add directory dir to the list of directories to be searched for -l.

 

https://gcc.gnu.org/onlinedocs/g...

 

So I guess it should also work as "-L." - without the space ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

 

Hey YJQ! Please don't put angle brackets ( < > ) around filenames in posts here! (Unless, of-course it's a filename in an #include directive in a code block.)

 

It upsets the forum software somewhat, and quoting such text becomes awkward.

 

Sure ! Thanks for reminding.  

Rookie

Last Edited: Tue. Dec 12, 2017 - 11:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
So I guess it should also work as "-L." - without the space ?
Well yeah, but I think that using -L or -I without a space looks "messy". It's a bit like writing C without spaces on either side of each operator.

 

BTW I would still question the merit of static libs. Unless liblcd.a is deliberately "hidden" as binary only then what's to be gained by using a static lib for an AVR? If this is an HD44780 support library then if you move it to some other model of AVR isn't there a chance you need to re-assign the pins used (at the very least) ? If they are hard coded in some binary how will you achieve that? Or does the library really take PORT/pin parameters during the init to say which lines are to be used? If it does then is that not hugely inefficient?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
 I think that using -L or -I without a space looks "messy".

Agree - but then you have -l without a space...

 

The docs say a space is allowed with -l  - but deprecated.

 

frown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 

BTW I would still question the merit of static libs. Unless liblcd.a is deliberately "hidden" as binary only then what's to be gained by using a static lib for an AVR? If this is an HD44780 support library then if you move it to some other model of AVR isn't there a chance you need to re-assign the pins used (at the very least) ? If they are hard coded in some binary how will you achieve that? Or does the library really take PORT/pin parameters during the init to say which lines are to be used? If it does then is that not hugely inefficient?


​It's only my third month of the first year in uni, for this lab session we have to make mini games/programs on a 18-bit 240x320 TFT lcd display and we have been provided one liblcd.a and one liblcd.tar.gz (both unreadable using notepad/++ or microsoft word, by unreadable I mean bunch of garbage text and symbols) and a header file that contains all the functions declared, to be used for drawing on the display cartesian plane. All pins assignment have been 'fixed'. For the time being, we only need to know how and when to use the functions at hand to create images/animations, using other avr features such as interrupts as well. Perhaps it's more like drawing with java script.

 

Rookie

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So it sounds like your teachers have, as clawson said, "deliberately hidden" the source of that library from you - presumably so as not to distract you with it.

 

But it sounds like the instructions for using it were wrong/incomplete - you should feed this back to your teachers!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

YJQ wrote:
liblcd.tar.gz
  That is a zip file, you need an unzip program to access the contents, I recommend using 7-zip for windows.

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, "Zip" is a specific, proprietary format - different from both TAR and GZ

 

https://en.wikipedia.org/wiki/Zip_(file_format)

 

https://en.wikipedia.org/wiki/Tar_(computing)

 

https://en.wikipedia.org/wiki/Gzip

 

I recommend using 7-zip for windows

Yes - it understands them all.

 

(although Windows natively understands Zip these days)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Well, "Zip" is a specific, proprietary format - different from both TAR and GZ
What does the "z" in .gz stand for then?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wikipedia wrote:
gzip is not to be confused with the ZIP archive format

 

I guess it's (now) one of those names that has both a proprietary and a generic meaning?

 

Like my parents had an Electrolux hoover ... 

Last Edited: Tue. Dec 12, 2017 - 04:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Electrolux hoover
Oooh! Insulting both Electrolux and Hoover!smiley

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I should point out: In the past, I have in fact successfully compiled source code and linked it with libraries in a single command line. It's not usual practice, but it should work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

YJQ wrote:

2)  avr-gcc [...]  -llcd testc11.c -o testc11.elf

Libraries should *always* be specified after the modules.  (This won't affect how libraries are searched and won't affect your already solved problem, but symbol resolution may differ).

 

==> avr-gcc [...]  testc11.c -o testc11.elf -llcd

 

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

So it sounds like your teachers have, as clawson said, "deliberately hidden" the source of that library from you - presumably so as not to distract you with it.

 

But it sounds like the instructions for using it were wrong/incomplete - you should feed this back to your teachers!

Well just in case the professor ever saw this somehow (because I sent an email to ask him the same question but the reply was slow and I came to avr to ask for help).The source code was not actually hidden. We had been provided with a tar.gz file that I did not find out, until later, that it could be decompressed to show how functions are defined inside.

Rookie