I have looked at these links : This one
and This
and a couple of other similar ones.
None of the answers given here are working methods are working.
I have a two source files a1.c , a2.c and two header files a1.h and a2.h . I want to include the header files in both these files (and a2.c in a1.c as there is a function I need to use from a2.c)
I have included
#include "a1.h"
#include "a2.h"
in the source files of a1.c
I'm using GCC on Ubuntu. and using the command gcc a1.h -o a1.out -lm
and that didn't work.
I tried with
gcc -c -I/Home/Documents/ctests/ a1.c -o a1.out
as well as
gcc -c a1.c -I/Home/Documents/ctests/ -o a1.out
My spellings are okay as well (there's hardly any room for error there with one letter and a number as the filename anyway).
Also, all the files are in the same folder.
I know this may be a trivial question but I am stuck on this one and would appreciate any help. I am relatively new to programming and completely new to Linux and Unix as far as using the command line goes.
Many thanks!
gcc -c
tells gcc to compile the file to object (the .o files you see everywhere). To be linked later with some other .o files to an executable.
So what you want to do is either compile the two files separately and link them later. like this.
gcc -I"/Home/Documents/ctests/" -c a1.c
gcc -I"/Home/Documents/ctests/" -c a2.c
gcc -o myprogram a1.o a2.o
Or just compile and link at the same time.
gcc -I"/Home/Documents/ctests/" a2.c a1.c -o myprogram
And then run your program like
path_to/myprogram
Compile everything, and link it together.
If all files are in one directory, this should work:
gcc a1.c a2.c -o myapp
When you want to create separate object files, do this:
gcc -c a1.c a2.c
Then you can then link together to create an application:
gcc a1.o a2.o -o myapp
Your gcc command should be like this
gcc -I/Home/Documents/ctests/ -o a1.out a1.c
and you have to include a1.h and a2.h header file in your a1.c like this
#include "a1.h"
#include "a2.h"
If you are calling some function from a2.c in your a1.c then you have to build your program in this way
gcc -I/Home/Documents/ctests/ -o a1.out a2.c a1.c
Related
Essentially I have
header.h, tree.c, main.c, and list.c
Can someone tell me how to execute this in my vi terminal. I know to run a solo c file it's gcc list.c ...., but I need these linked and the header included. I just don't understand the format for running them all together as an executable with a linker. I've included header in all the files, but main relies on list and tree, and vice versa.
Any help trying to compile and run these with gcc in the terminal would be a big help. Whenever I try, it keeps running them separately and I get a bunch of errors.
You can compile (not run) and link these files into an executable in one step by passing each of the .c files to gcc together and using the -o option to give the name of the executable:
gcc -o myprogram tree.c list.c main.c
Or you can compile each of them to object files using -c:
gcc -c tree.c
gcc -c list.c
gcc -c main.c
And then link the resulting object files:
gcc -o myprogram tree.o list.o main.o
I found a useful library on github for my project, after building this later I tried to use some predefined function on it. I couldn't compile my project because there is some header file missing like this one :
In file included from main.c:2:0:
ptask.h:11:19: fatal error: ptime.h: No such file or directory
I compiled my project using this command :
gcc main.c -L. -lptask
This is all the files in project folder :
libptask.a main.c ptask.h
This is the library content:
$ ar -t libptask.a
pbarrier.c.o
pmutex.c.o
ptask.c.o
ptime.c.o
rtmode.c.o
tstat.c.o
libdl.c.o
dle_timer.c.o
calibrate.c.o
Do I need to add all the headers of this files or just link the lib when compiling ?
Your main.c #include-s ptask.h which in turn #include-s ptime.h. Having compiled static libs alone is not enough (that's the linker's job), you still need to have all used header files (which is the compiler's job), both the ones you use and their dependencies, recursively applicable.
Normally you need to be sure that the header files are in your "include path", something that a lot of compilers define with -I as a command-line option. You'll need to include the source directory of that library, or if it has a make install option, then the place where they got installed.
regarding:
gcc main.c -L. -lptask
this is performing the compile step and the link step in one command.
It is also not enabling the warnings, which should always be enabled during the compile step.
Suggest something similar to the following to compile
gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -g -c main.c -o main.o -I.
and when you have fixed all the warnings, then use something similar to the following to link
gcc main.o -o main -L. -lptask
I wrote a C programm and saved it with a .c extension.
Then I compiled with the gcc but after that I only see my .c file and an .exe file. The program runs perfectly. But where is the .o file that I learned in theory? Has it been overwritten to .exe and all done by the gcc in on step? (Preprocessing, compiling, assembling and linking)
I'm on a VM running Debian.
By default, gcc compiles and links in one step. To get a .o file, you need to compile without linking. That's done with the -c option.
Suppose you want to compile two files separately, then link them. You would do the following:
gcc -c file1.c # creates file1.o
gcc -c file2.c # creates file2.o
gcc -o myexe file1.o file2.o
If you want just the output of the preprocessor, use the -E option along with the -o to specify the output file:
gcc -E file1.c -o file1-pp.c # creates file1-pp.c
Compile and link in two steps:
gcc -Wall -c tst.c
gcc tst.c -o tst
After first command you'll get a .o file.
if you did something like gcc test.c then it produces only the executable file (in order to compile only, see the -c option)
here is steps on compiling with gcc to create a .o file from your C file:
http://www.gnu.org/software/libtool/manual/html_node/Creating-object-files.html
I have written c program, Which has 3 file(.c ) , Main program has
two threads and one file has mysql connection function, One file has
thread functions definition. I don't know how to compile all these
codes, Normally I tried like this,
cc -pthread main.c
But if I compile like this I am getting error called mysql functions
are undefined But I have written thread as separate program and
mysql as separate program and complied individually , it complied
successfully and I got output. So please help me to compile my
project File names are,
main.c (2 threads are declared) functions.c (thread function
definition, and mysql func declared) db.c ( mysql function
definition)
please help to compile my code?
You have two basic options when compiling multiple .c files:
Option 1)
cc file1.c file2.c file3.c -o executable_name
Advantage: simple
Disadvantage: if all you change is one file you are recompiling all the files
Option 2)
cc file1.c -c -o file1.o
cc file2.c -c -o file2.o
cc file3.c -c -o file3.o
cc file1.o file2.o file3.o -o executable_name
Advantage: If you change one file you do not have to recompile everything
Disadvantage: Multiple commands (but you should use a Makefile at this point)
The -c flag tells the compiler to compiler but not link. You don't want to link as you have not compiled all of your files. The final invocation of cc links all the .o files into the executable executable_name
It is a little bit difficult to understand exactly what you need, but I can tell you from what you've stated that you'll need to include specific libraries in your compile statement you currently are not. Also, a -l flag needs to prefix your libraries.
Try something like this:
gcc -lpthread main.c functions.c db.c -o main $(mysql_config --libs)
To explain, mysql_config --libs returns all the configuration libraries needed to run mysql ddl inside your C program.
Given your updates on your file declarations I'm guessing you're a Java programmer. C is not Java. If you are declaring functions you are only going to use once in main.c you should put them inside main.c unless you need them to be portable.
I have a custom header file example.h which has prototypes for a few functions. There is a .C file example.c that I implemented which "includes" (#include "example.h") and has the implementations of the functions that has prototype in example.h.
Now, I have another function test.c that has to call the functions that are prototyped in example.h and are implemented in example.c. How Can I do it?
You need to link them all at the end (assuming you have already included the prototypes into your test.c). So if you're compiling, you can compile both of the .c files together into one executable. More commonly, however, is to compile these without linking (which produces object files). Then, at the end, link all of the object files together. To do this depends on your compiler, but an example would be:
gcc -c -o example.o example.c
gcc -c -o test.o test.c
gcc -o my_application test.o example.o
Or, for a small project, this works just as well
gcc -o my_application example.c test.c
Just #include "example.h" in test.c (and don't forget to link all of the object files!)