LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2010, 12:19 AM   #1
bvkim
LQ Newbie
 
Registered: Apr 2010
Posts: 23

Rep: Reputation: 15
Question how to setting for my own header file and library?


I have 3 files: main.c, mylib.h, mylib.c

Now I want to put mylib.h into : /usr/include/mydir/mylib.h
And I create a static library: libmylib.a, and put into the folder: /usr/lib/mydir/libmylib.a

Then I compile: $ gcc -o main main.c
Then I got linking error
Code:
main.c:(.text+0x3e): undefined reference to `extract_v1'
main.c:(.text+0x7b): undefined reference to `modify_v1'
collect2: ld returned 1 exit status
So I try with -l options: $ gcc -o main main.c -lmylib
I still got error
Code:
/usr/bin/ld: cannot find -lmylib
collect2: ld returned 1 exit status
Any idea about this?

Also, I want to ask about: how to create my own header and library to put into /usr/include/mylib/ and /usr/lib/mylib/, so when I use function in my program, the compiler will automatically link to library.
It's like when you #include <stdio.h>, and you compile: $ gcc -o program program.c, you don't need to specify any linking folder or library.
 
Old 05-08-2010, 02:11 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
How did you go about creating the library?
Did you put /usr/include/mydir/ in the search path for libraries?
How to make and use libraries in gcc ... for djgpp, but clear. Also see gcc documentation.
 
Old 05-08-2010, 02:14 AM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code?
 
Old 05-08-2010, 07:07 AM   #4
hda7
Member
 
Registered: May 2009
Distribution: Debian wheezy
Posts: 252

Rep: Reputation: 31
To get gcc to find your library, you must add the folder it is in to gcc's search path:
Code:
gcc -o main main.c -L/usr/lib/mydir -lmylib
Because you are linking a static library, you don't have to change LD_LIBRARY_PATH (for runtime loading) only the link path.
 
1 members found this post helpful.
Old 05-08-2010, 10:38 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
To summarize, and clarify: gcc needs to be told what libraries to link, and this is done with the '-l' (lower-case ell) flag followed by the name of the library (but without the prefix 'lib'; go figure). It also needs to be told where to look for any libraries that it wants to use, and this is done with one or more '-L' arguments, each of which specify a path, and which collectively comprise an ordered list of paths to search for libraries. The '-I' (uppercase eye) argument is used analogously to the '-L' argument by the compiler to find header files.
Having said this, the compiler has built-in lists of standard places to look for things, and also what standard libraries should be linked. You can only get the compiler to look in other places by telling it to do so, using the switches mentioned above. So, you could put your libraries and header files in one of the 'standard' places, but this is generally considered bad form. I suppose you could re-build your toolchain, and add to the lists of standard places, but this sounds like a lot more work than just adding some commandline switches to your Makefile. Moreover, the CFLAGS & LDFLAGS variables in your Makefile serve as a form of documentation. They spell out what libraries are used and where they can be found. Header files that are created as part of a project are usually stored in the same directory as the C source code. These kinds of things are conventions that other programmers learn to expect, and going to another arrangement would probably not be a good idea, except in special circumstances.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setting the header file path in MAKEFILE shariefbe Linux - Software 6 12-24-2009 03:00 AM
MP3Lame header and library linx win Debian 4 12-18-2005 09:13 PM
netbsd c library and header files >minimalist< Programming 0 10-31-2005 09:08 PM
ld cannot find library/header which _is_ there. jmorse Linux - Software 3 06-27-2004 04:18 PM
How do I custom write a library and use it in the header declaration ? Linh Programming 4 05-26-2004 03:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration