Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-12-2006, 02:28 PM   #1
Registered: Jun 2006
Posts: 60

Rep: Reputation: 15
Compiling C in Linux

If I want to compile the source code for a command (say cp.c) do I have to copy all the included files in the same directory of the source code? It would be a problem because the source code contains too many #include-s!!
Old 12-12-2006, 03:33 PM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060

There are two ways to include #include files:
#include <stdio.h>
#include "local.h"
Where "<stdio.h>" tells the compiler to look for the file in the /usr/include directory tree and "local.h" tells it to look in the current directory (using -Ipath-to-directory lets you tell the compiler to look for header files there).

Before you go too far down the C path, you may want to check or your local bookseller for a good introductory text.
Old 12-12-2006, 03:40 PM   #3
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
It depends on how you've configured your system. If you've used a default configuration, the include search paths should be defined, and "standard" system include files should not need to be copied to your "work" directory. If you've used your own configuration, you're -- of course -- "on your own."

If you're new to Linux and/or compilation on Linux distributions, you should try one of the IDEs available with most distributions. Or designed for specific "desktop" environments (e.g., GNOME or KDE or ...).
Old 12-12-2006, 06:41 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You may find this useful:
Old 12-12-2006, 07:14 PM   #5
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you have an include file in a non-standard path, e.g. /opt/myincludes, you can include them with the -I option to gcc:
gcc -I/opt/myincludes -c cp.c -o cp.o
If you have includes from multiple directories, you can use more than one -I option:
gcc -I/opt/myincludes -I/opt/yourincludes ...
Note that when linking, you may need to link with libraries which are not in the default library path. You can include these library paths with the -L option. e.g. if you have a library file you need to link to: "/opt/mylibs/", you would do it like this:
gcc -L/opt/mylibs -lmine cp.o -o cp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
compiling c++ in linux deapee Programming 14 08-07-2012 12:05 PM
Compiling Linux 0.0.1? Vampirite Slackware 22 11-03-2008 04:28 AM
compiling linux FMAlchemist Linux - General 3 09-21-2006 07:06 AM
Various Compiling Errors (GCC compiling Openal, GUIlib, xmms-wma) gregorya Linux - Software 2 08-27-2004 06:03 AM
Compiling in ACPI support on Compaq 2135CA (system crashes while compiling) Dag Linux - Laptop and Netbook 20 07-30-2004 08:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:32 PM.

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