LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compiling C in Linux (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-c-in-linux-509774/)

drimades 12-12-2006 01:28 PM

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

tronayne 12-12-2006 02:33 PM

No.

There are two ways to include #include files:
Code:

#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 Amazon.com or your local bookseller for a good introductory text.

PTrenholme 12-12-2006 02:40 PM

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

chrism01 12-12-2006 05:41 PM

You may find this useful:
http://www.advancedlinuxprogramming.com/resources.html

matthewg42 12-12-2006 06:14 PM

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:
Code:

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:
Code:

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/libmine.so", you would do it like this:
Code:

gcc -L/opt/mylibs -lmine cp.o -o cp


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