LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-26-2010, 06:39 PM   #1
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Rep: Reputation: 40
Where do I put header files for a shared library I have written?


I have written a simple library and ended up with a .so file. I have a header file from writing the code that describes how to use the functions in the source code I have written. I think this .h files needs to be available to other programs that access this code.

I have seen lots of tutorials on how to copy the .so file to the relevant directories and make links with the version number. What I can't find is where to put the header file so that any programs I write to use my new library can access the header.

Hope this makes sense. For example, I might use <stdio.h> normally, I will need to access <mylibrary.h> once mylibrary.so is loaded (as far as I understand!)

It's weird, I've been using C compilers for embedded processors over ten years now and never given a second thought to how libraries and headers work behind the scenes!

Many thanks.
 
Old 02-26-2010, 08:20 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
The "standard" places for headers are in "/usr/include" and "/usr/local/include". If you're using gcc, the "-I" option can be used to pass additional include paths, if it isn't in one of the default locations that it looks. Other compilers will have similar options.
 
1 members found this post helpful.
Old 02-26-2010, 11:25 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

For whatever it's worth, I would strongly suggest putting the header(s) in whatever "project directory" is convenient for you, and use "-I".

IMHO .. PSM

PS:
The client code should generally use double-quotes, not brackets:[code]#include "myheader.h"[/quote]

PPS:
If you haven't already done it, you might want to put "guards" in your header. For example:
[code]
/*
* My header file
*/
#if !defined(MY_HEADER_H)
#define MY_HEADER_H
...
#endif
/* MY_HEADER_H */[code]

Last edited by paulsm4; 02-26-2010 at 11:28 PM.
 
Old 02-27-2010, 10:56 AM   #4
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Original Poster
Rep: Reputation: 40
Thanks, folks, that helps.

Paul, I had, indeed, done the #define thing in the headers. I thought that was something C++ insisted on, though I could well be talking out of my hat.
 
Old 02-27-2010, 11:45 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
I had, indeed, done the #define thing in the headers. I thought that was something C++ insisted on
Actually, it's just a "preprocessor thing". Handling the #include's, #ifdef's, and #define's occurs before either C or C++ see a line of code.

The purpose of these "guard's" is to make sure a header gets #included'ed once - and only once.

Otherwise, if A.cpp #include both A.h and B.h, and B.h also #include's A.h, then everything in A.h would be duplicated. Not good. The "guard" syntax prevents this from happening.

'Hope that helps .. PSM
 
Old 02-28-2010, 12:38 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
generally one wouldn't put header files in system directories,
they are only needed during the compilation phase.
Unless maybe you have produced some sort of public library or SDK other people
will compile against. something widespread in the community.


plus they'll get clobbered if you install a new OS
unless you have them in 2 places which is a recipe for inconsistency.
 
Old 02-28-2010, 12:45 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If someone builds a program using your library, they will download your source or at least the -devel version of your package.
An end user won't need the header files, but your binary library package will be a dependency.

If you ever installed packages from source, you will notice that you also need the -devel packages for the same dependencies that the binary package uses. The main package will contain and install your .so library. A *-devel package will install the static version of your library and the header files.

For -devel packages that your distro installs, the library is installed to /usr/lib and the -devel libraries and files are installed to /usr/include in a directory named after the library. For example:
Code:
rpm -ql libspeex-devel
/usr/include/speex
/usr/include/speex/speex.h
/usr/include/speex/speex_bits.h
/usr/include/speex/speex_buffer.h
/usr/include/speex/speex_callbacks.h
/usr/include/speex/speex_config_types.h
/usr/include/speex/speex_echo.h
/usr/include/speex/speex_header.h
/usr/include/speex/speex_jitter.h
/usr/include/speex/speex_preprocess.h
/usr/include/speex/speex_resampler.h
/usr/include/speex/speex_stereo.h
/usr/include/speex/speex_types.h
For a non-distro package installed from source, using /usr/local/ as the base for lib & include would probably be what you want to use. E.G.
/usr/local/lib/libbosco.so

/usr/local/include/bosco/libbosco.h

Last edited by jschiwal; 02-28-2010 at 12:56 PM.
 
  


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
shared library what is .soT? files Ashok_mittal Programming 1 03-05-2009 12:32 AM
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 11:15 AM
where do i put my written scripts mahmoud Linux - Newbie 2 11-27-2007 05:42 PM
How to see of which objects files is particular shared library made of? halturata Programming 4 04-12-2006 01:35 AM
netbsd c library and header files >minimalist< Programming 0 10-31-2005 09:08 PM

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

All times are GMT -5. The time now is 03:55 AM.

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