LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2012, 10:38 AM   #1
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: 0
compiling a C program with header files in a separate directory


My question is an indication of how new I am to Linux.

I have a small program "main.c" in my cctools directory. It's header files are in cctools/include/cctools.

How do I write the path to compile this little program? I've sought out tutorials, and I've tried the following but it did not work.

gcc -Wall -I/include/cctools -L/include/cctools/lib main.c hello_fn.c -o newHello -lhello

What do I do?
 
Old 06-08-2012, 10:45 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Wierd directory layout...
You just have to specify an absolute or relative path to the header files and the libraries with the -I and -L switches.
Try remove the first / from both paths, as you're telling gcc to search for include files and libraries under /include, which I doubt exists on your system.
 
1 members found this post helpful.
Old 06-08-2012, 10:48 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Remove the leading slash from your include and lib flags. When you put /include/cctools, the compiler looks in /include, not the include subdirectory of wherever you are. In the windows world, /include would be like C:\include. Unless your include is actually located in / (doubtful), that leading slash is telling it to look in the wrong place.

Edit: too slow
 
1 members found this post helpful.
Old 06-08-2012, 10:48 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
in general you need to use either full path or relative path. Full path always begin with slash and starts at the root of the filesystem, relative paths starts with something else (usually with ./<dir> or ../<something> or <dirname>) which can be evaluated as a path from the current dir. You can always check if the given path valid by executing ls <path>. /include/cctools and /include/cctools/lib are probably both invalid paths in your example.
 
1 members found this post helpful.
Old 06-08-2012, 12:48 PM   #5
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Thank You!

Removing the leading "/" worked like a dream! Although it did generate an error stating:

"cannot find -lhello"

My new problem is that it did not create my executable file "newHello."

How do I get that to work?

Last edited by emaritza; 06-08-2012 at 12:51 PM. Reason: incomplete response
 
Old 06-09-2012, 01:23 AM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by emaritza View Post
Removing the leading "/" worked like a dream! Although it did generate an error stating:

"cannot find -lhello"

My new problem is that it did not create my executable file "newHello."

How do I get that to work?
Do you have a file named libhello.so inside your lib directory?
 
Old 06-12-2012, 05:47 PM   #7
emaritza
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
I do not.

I shall add that and try again.

Thank you for the insight!
 
  


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
c program compiling with header file ant___man Programming 4 12-15-2011 03:53 PM
[SOLVED] compiling kernel, firmware_install, prefer separate directory Andy Alt Slackware 4 12-06-2010 12:33 PM
Missing header GLTK in compiling an openGL program BorgAssimilator Programming 1 01-23-2006 09:07 PM
Compiling C++ Header Files ?? JinBaba Programming 3 07-20-2005 03:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:41 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