Compiling in gcc dynamically with objects in other folders - c

I am developing a project with an ordeneted use of folders. I want a folder called /bin to store all the binary files and a folder called /src with all the .c and .h files.
The program will solve system of equations and all the functions related to matrix calculations will be compiled dynamically.
I have this folders:
/project/src/matrixlib (it will be the fynamic lybrary with all the matrix calculations)
With files:
matrix.c
#include<stdio.h>
#include"matrix.h"
int matrix_alloc(int n, int m, Matrix *matrix);
{
//do whatever, this is not the problem
}
matrix.h
#ifndef matrix_h__
#define matrix_h__
struct Matrix
{
//definition here
};typedef struct Matrix Matrix;
extern int matrix_alloc(int, int, Matrix*);
#endif //matrix_h__
I compile this folder with:
gcc -c -Wall -Werror -fpic matrix.c
gcc -shared -o libmatrix.so matrix.o
Then I have the folder:
/project/src/main
With files:
main.c (using function "matrix_alloc" and including "matrix.h")
#include<stdio.h>
#include "matrix.h"
int main(void)
{
Matrix matrix;
matrix_alloc(3,3,&matrix);
return 0;
}
and I compile like this:
gcc -I/project/src/matrixlib -L/project/src/matrixlib -Wl,-rpath=/project/src/matrixlib -Wall main.c -o main
But I've got this error:
/tmp/cc8kLMIe.o: In function `main':
main.c:(.text+0x34): undefined reference to `matrix_alloc'
collect2: error: ld returned 1 exit status
But I don't really understand what is going on because matrix_alloc is in matrix.h folder.
Could you help me please?
Thanks

You're not linking libmatrix.so with your program. Add -lmatrix to your compiler invocation

Related

Installing and using a shared library

I have a project I'm working on that currently has a matrix.c file containing a some functions and has a corresponding header matrix.h. I made a shared library libccalc.so using matrix.c and another file. I am trying to create a directory in /usr/local/lib and a directory in /usr/local/include, both called ccalc to house the respective .so and .h files which can then later be used to compile programs using the functionality of libccalc.so. However, when I do this I am getting an error.
To be precise, matrix.c contains functions:
Matrix *mat_create(int rows, int cols),
void mat_fill(Matrix *mat, double *entries) and
void mat_print(Matrix *mat)
which are declared in matrix.h. I place the files in their respective directories as explained above, I run ldconfig /usr/local/lib/ccalc and I make a new file test.c in some other directory with the following:
// test.c
#include <stdio.h>
#include "matrix.h"
int main() {
Matrix *m = mat_create(2, 2);
double entries[4] = {1, 2, 3 ,4};
mat_fill(m, entries);
mat_print(m);
return 0;
}
matrix.h contains the following:
// matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#define MAX_SIZE 50
typedef struct Matrix Matrix;
struct Matrix {
int rows;
int cols;
double *data;
};
Matrix *mat_create(int n, int p);
void mat_fill(Matrix *mat, double *entries);
void mat_print(Matrix *mat);
#endif
When I enter the command: gcc -I/usr/local/include/ccalc -L/usr/local/lib/ccalc -lccalc test.c -o test, I get the error:
/usr/bin/ld: /tmp/ccesD44J.o: in function `main':
test.c:(.text+0x26): undefined reference to `mat_create'
/usr/bin/ld: test.c:(.text+0x71): undefined reference to `mat_fill'
/usr/bin/ld: test.c:(.text+0x7d): undefined reference to `mat_print'
collect2: error: ld returned 1 exit status
However, when I place libccalc.so and matrix.h in /usr/local/lib and /usr/local/include, enter ldconfig and enter the command gcc -L/usr/local/lib/ccalc -lccalc test.c -o test, and run ./test, it works perfectly
Can someone tell me what I am doing wrong?
The order of program arguments to gcc matters a lot. Please read the documentation about Invoking GCC.
Also, test is often a builtin command. See test(1).
So (on Linux) rename test.c as prog.c then compile your program with
gcc -Wall -Wextra -g prog.c -L/usr/local/lib/ccalc -lccalc -o prog
Then use strace(1) and gdb(1) to understand the behavior of prog, and ldd(1), objdump(1) with readelf(1) to analyze prog. See also elf(5) and ld.so(8).
Regarding building shared libraries, read Drepper's paper How to write shared libraries
You probably want to compile matrix.c into libcalc.so using
gcc -fPIC -Wall -O -shared -g matrix.c -o libcalc.so
but you might need other program arguments.
Regarding matrix operations, this answer could be inspirational.

C - Makefile compiles after header file changes, but changes dont take effect

I have 3 files in this program, lab4.c, functions.h, functions.c
The lab4.c calls a function multiply(), whose prototype is in functions.h and is defined in functions.c. Multiply then used multiple other functions from functions.c. The only includes I have for this is in lab4.c including functions.h, do I need more? The problem I am having is described below
lab4:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
...
}
functions.h:
#ifndef FUNCTIONS
#define FUNCTIONS
void divideByPowerOf2(unsigned int* a, int power);
void multiplyByPowerOf2(unsigned int* a, int power);
...
#endif /* FUNCTIONS */
functions.c:
void divideByPowerOf2(unsigned int* a, int power){
*a >>= power;
}
void multiplyByPowerOf2(unsigned int* a, int power){
*a <<= power;
}
...
Currently, my makefile looks like this:
Makefile:
#Makefile
all: lab4
lab4: lab4.o functions.o functions.h
gcc -Wall -o lab4 lab4.o functions.o
lab4.0: lab4.c
gcc -c lab4.c
functions.o: functions.c
gcc -c functions.c
now this will recompile when I change the header file, but the changes dont actually take effect. For example, if I change the header file to
#ifndef FUNCTIONS
#define FUNCTIONS
void divideByPowerOf2(unsigned int* a, int power);
//void multiplyByPowerOf2(unsigned int* a, int power);
...
#endif /* FUNCTIONS */
the program still works just fine. Im assuming I may have messed up linking the files with includes and everything, as that usually confuses me. For example, does functions.c need to refer to anything? and does functions.h need any kind of reference to the .c files? How do I get this to work properly so that if I change the header file, it recompiles and actually uses the new header
Thanks for any help!
First, there's a typo here:
lab4.0: lab4.c
should be
lab4.o: lab4.c
then, your function.h should be on the source => object dependency lines, not on the object => executable line, else, if you change the .h file, it just re-links without rebuilding the .o files: it changes nothing.
Moreover, it's good to use -Wall, but you have to use it when you compile your files, not when you link the executable, or you'll miss the actual compilation warnings (-Wall during the link phase only is pretty useless).
For instance, the -Wall flag would show you that commenting a prototype generates an "implicit declaration" warning (which can lead to an improper call/return values of a function). It's even more effective with -Werror, which turns warnings into errors, so you cannot ignore warnings.
Here's how your makefile should look like:
all: lab4
CFLAGS = -Wall
lab4: lab4.o functions.o
gcc -o lab4 lab4.o functions.o
lab4.o: lab4.c functions.h
gcc $(CFLAGS) -c lab4.c
functions.o: functions.c functions.h
gcc $(CFLAGS) -c functions.c
note that if you only have 2 source files and they're small enough, you could even not use the make file by just running:
gcc -Wall -o lab4 lab4.c functions.c
in a script. That's not adapted if you have too many / big source files, because it rebuilds everything everytime.

Cuda mixed C project linking

I have a large project in C and i'm trying to integrate some Cuda kernels in it. I'm compiling my c-files with "gcc -c main.c" and my .cu files with "nvcc -c cuda_GMRES.cu" and then I try to link the 2 object files with nvcc: "nvcc -o main.o cuda_GMRES.o" and receive the following error:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function
_start':
(.text+0x20): undefined reference tomain'
collect2: ld returned 1 exit status
It's the first time I'm trying to combine cuda with C files and I might have done something wrong.Can someone help me please. I'm on a GPU Cluster with Rocks OS.
My main.c file:
#include <stdio.h>
#include <math.h>
#include "cuda_wrapper.h" //header containing wrapper function
//cuda_GMRES that calls the kernel cuda_dot
int main (int argc,char* argv[])
{
//content
//bla bla bla
//cuda Function call
cuda_GMRES(50);
return 0;
}
My cuda_wrapper.h file:
#ifndef Cuda_GMRES_cuda_wrapper_h
#define Cuda_GMRES_cuda_wrapper_h
//wrapper function declaration
void cuda_GMRES(double a);
#endif
My cuda_GMRES.cu file that contains the kernel calling function:
#include <stdio.h>
#include "cuda_wrapper.h"
#include "cuda_dot.cu"
//kernel declaration
__global__ void cuda_dot();
//kernel calling function
extern "C"
void cuda_GMRES(double a)
{
double b;
double *dev_a;
double *res;
cudaMemcpy(dev_a, &a, sizeof(double), cudaMemcpyHostToDevice );
cuda_dot<<< 1, 1 >>>(*dev_a, res );
cudaMemcpy(&b, res, sizeof(double), cudaMemcpyDeviceToHost );
}
My cuda_dot.cu file that contains the kernel:
__global__ void cuda_dot(double a, double *help)
{
*help=2*a;
}
Your linking command appears to contain a fatal error. Supposing you first compile two objects like this:
gcc -c main.c
nvcc -c cuda_GMRES.cu
you should have two object files main.o and cuda_GMRES.o. You then do this:
nvcc -o main.o cuda_GMRES.o
This command says "link a program file called main.o using cuda_GMRES.o", ie. overwrite main.o. It is for this reason that the linker is complaining about a missing main subroutine, you are not supplying one (and you are destroying the object file which contains one at the same time).
You want something like this:
nvcc -o executable main.o cuda_GMRES.o
where executable is the name of the final linked program, or
nvcc main.o cuda_GMRES.o
which will emit a default linked program called a.out

Why does CC not see my function definition in header?

I'm writing a simple application in ANSI C. I am using GCC in a Unix environment.
I have the following sample application:
//main.c
#include "foo.h"
int main()
{
int result;
result = add(1,5);
return0;
}
Header:
//foo.h
#ifndef FOO_H_INCLUDED
#define FF_H_INCLUDED
int add(int a, int b);
#endif
Implementation:
//foo.c
int add(int a, int b)
{
return a+b;
}
I am compiling my program with the following command:
cc main.c -o main.o
The compiler complains that 'reference to add is undefined'. Is this a linking problem? How do properly make use of my header?
Thanks!
You need to compile both your source files together:
cc main.c foo.c -o main
Also, in this case, -o produces an executable, so calling it main.o can be misleading.
Yet another tidbit, though unrelated to the question: the #ifndef and #define in foo.h don't match.
The header is not your current problem. Your current problem is that you're not compiling the add function definition in foo.c.
Try
cc main.c foo.c -o main.o
If you are trying to compile main.c into an assembled object file, you need to prevent gcc from trying to link. This is done via
cc -c main.c -o main.o
You can compile all other object files, then when you have all of your object files ready, you simply do
cc main.o obj1.o anotherOBJ.o -o myExecutableBinary
"undefined reference" is a linker error, not a compiler error.
The compiler sees the declaration in the header, but you have not compiled or linked the definition in foo.c. Your title uses the term definition incorrectly.

Include a source file in a C program

How can I include foo() function of foo.c in this small program (sorry for my noob question):
In my foo.h file:
/* foo.h */
#include <stdio.h>
#include <stdlib.h>
int foo(double largeur);
In foo.c:
/* foo.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(double largeur)
{
printf("foo");
return 0;
}
And in main.c:
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int main(int argc, char *argv[])
{
printf("Avant...");
foo(2);
printf("Apres...");
return 0;
}
After compiling:
$ gcc -Wall -o main main.c
I get this error:
Undefined symbols: "_foo",
referenced from:
_main in ccerSyBF.o ld: symbol(s) not found collect2: ld
returned 1 exit status
Thanks for any help.
$ gcc -Wall -o main main.c foo.c
GCC doesn't know to look for foo.c if you don't tell it to :)
Creating a program in C requires two steps, compiling and linking. To just run the compiling part, use the -c option to gcc:
gcc -c main.c
This creates an object file, main.o (or main.obj on Windows). Similarly for gcc -c foo.c. You won't get the error message above at this stage. Then you link these two object files together. At this stage, the symbol foo is resolved. The reason you got the error message was because the linker couldn't find the symbol, because it was only looking at main.o and not foo.o. The linker is usually run from gcc, so to link your object files and create the final executable file main, use
gcc -o main main.o foo.o
You have to compile foo.c also because it is another module. Let me see how they do it in gcc:
$ gcc -Wall main.c foo.c -o main
You could also do this in your MakeFiles, like this:
APP_NAME = Foo
Foo_HEADERS = foo.h
Foo_FILES = main.c foo.c
If you're not so much familiar with MakeFiles i suggest you to take a look at Make Docs, but this is a simple example, APP_NAME sets the name of the compiled executable(in this case is Foo), Foo_HEADERS will set the headers used by your application, Foo_FILES you will set the source files of your applications, remember to put the APP_NAME(in this case Foo) at the beginning of _HEADERS and _FILES. I suggest you to use MakeFiles because they will organize you application build process and will be better for the end-user.

Resources