Trying to make a simple Makefile fails - c

I have 3 files, main.c, lists.c, and lists.h.
Im trying to write a Makefile with all the files are in the same directory:
maman21: lists.c lists.h main.c
gcc -g -Wall -ansi main.c -o maman21 -lm
going to the folder through terminal and using make shows me this message:
gcc -g -Wall -ansi main.c -o maman21 -lm
Undefined symbols for architecture x86_64: "_linkedListWay",
referenced from:
_main in main-C9dUT4.o "_reallocWay", referenced from:
_main in main-C9dUT4.o ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to
see invocation)
make: *** [maman21] Error 1
reallocWay and linkedListWay are to functions I'm using in the file.
Thank you for your help.

You've failed to include the lists.c file in the compiler invocation so it doesn't get built.
It should be:
CFLAGS=-g -Wall -ansi
LDLIBS=-lm
maman21: main.o lists.o
main.o: main.c
lists.o: lists.c lists.h
The above uses implicit Makefile rules, it "knows" how to convert a C file to an object (.o) file.
Also, is it normal for Clang to be called gcc?

Related

linker command failed while trying to compile .c file with g++

Just posted a simple c code and gotten this error message. can anyone explain where the problem could be?
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mac#MACs-MacBook-Pro-2 C % cd "/Users/mac/C/" && g++ tes.c++ -o tes && "/Users/mac/C/"tes
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
you have to compile with gcc and not g++, g++ is to compile .cpp or .cc files
gcc tes.c++ -o tes

Can gcc -o and -S be used together

Can I use for example
gcc -o -S output.s abs.c
to generate an assembly file with name output.s? It seems like I can't. When I try to do that, I got following error message.
Undefined symbols for architecture x86_64: "_main", referenced from:
implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit
code 1 (use -v to see invocation)
I do not intend to use the linker, just try to examine the assembly code.
-o must be followed by the name of the output file. So, this would work:
gcc -S abc.c -o output.s

C program Compilation error : reference to main

I am new to C programming and need help in resolving the compilation issue :
There are 3 .c files (main.c, file1.c , file2.c) file1.c and file2.c just contain the function definitions that are called in main.c. When I am trying to compile my main.c file using the below command, it gives me the errors:
gcc -Wall ./trigger-solve/main.c
_approx_help", referenced from:
_print_help in main-9e2c6e.o
"_approximate", referenced from:
_main in main-9e2c6e.o
"_free_matrix", referenced from:
_main in main-9e2c6e.o
"_print_matrix", referenced from:
_main in main-9e2c6e.o
"_read_matrix", referenced from:
_main in main-9e2c6e.o
"_read_sparse_matrix", referenced from:
_main in main-9e2c6e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However after reading about this error , I figured out its happening because file1.c and file2.c do not contain the main function . So I added this main function in both files :
int main()
{
return 1;
}
At this point when I compile file1.c and file2.c they DO NOT give me errors , however I am still getting the same compilation errors while running main.c. Also I don't know after compiling file1.c and file2.c how should I link them to main.c ?
Can someone please help .
You need to link all your translation units together. Perhaps all in one wash, like this:
gcc -Wall main.c file1.c file2.c
Now you have an executable file called a.out ready for your enjoyment.
Of course rebuilding the entire project every time you change one file isn't feasible, so normally you'd compile each TU separately and then link everything in a final step (and give the output file a better name):
gcc -Wall -c main.c
gcc -Wall -c file1.c
gcc -Wall -c file2.c
gcc -o myprog main.o file1.o file2.o
Did you do any research yourself on this? There are plenty of relevant examples available online.
You should not define main function in file1.c and file2.c. You should compile all .c files with -c option and then link them to main like this :
gcc main.o file1.o file2.o
you can just
gcc -Wall *.c
will compile all .c files in the current directory... thats a good shortcut if you save your each of your project in a separate folder
Basically, you have got the linking error. Remember, any program has only one main() function. In your example, this main() must be finally linked with the other files to create an executable. The main() is the entry point of any C program. Thus, there should be only one main() in your source code in order to create an executable. For your case, I will say use the Makefile for compiling and linking. The makefile saves time of compilation and is quite handy.
I will say - use the Makefile. The Makefile makes the job easier. In future, if you have to extend the functionality, add a new file and add a new entry in the makefile. This is the simplest form of makefile here.
Once you issue make - the executable name - app will be created. Next, run the ./app
Makefile
target = file1.0 file2.o main.o
[tab] gcc file1.o file2.o main.o -o app
file1.o: file1.c
[tab] gcc -c file1.c -o file1.o
file2.o: file2.c
[tab] gcc -c file2.c -o file2.o
main.o: main.c
[tab] gcc -c main.c -o main.o
Now, run the Makefile.
$make target

How to compile gcc 32bit app on 64bit osx

i try to compile a 32 bit app on my mac osx 64bit
I have a 32bit lib included.
I try to create a personal lib
gcc -m32 -c fileA.c -Iinclude -o fileA.o
gcc -m32 -c fileB.c -Iinclude -o fileB.o
All Ok
now I create .a File
ar ruv ./lib/myLib.a fileA.o fileB.o
When I try to launch
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib
I receive the following error
Undefined symbols for architecture i386: "_addozzo", referenced
from:
_main in imageMod-nfyyGP.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1
(use -v to see invocation)
anyone can help me?
You need to tell gcc to link in the library:
gcc -m32 -o imageMod imageMod.c -Iinclude -Llib -l:myLib.a

Eclipse build configuration for OpenMP

I am trying to learn OpenMP, starting with the following simple snippet
#include <stdio.h>
#include <stdlib.h>
int main(void) {
#pragma omp parallel
printf("Hello OpenMP!\n");
return 0;
}
Simply compiling from the command line works:
cls ~/Desktop $ gcc -fopenmp HelloOpenMP.c -o HelloOpenMP
cls ~/Desktop $ ./HelloOpenMP
Hello OpenMP!
Hello OpenMP!
However, I'd like to use Eclipse with CDT. I created a new build configuration "OpenMP" and tried to add the -fopenmp flag under "Miscellaneous", copying the other settings from the "Debug" build configuration.
The build fails with
14:56:16 **** Incremental Build of configuration OpenMP for project HelloOpenMP ****
make all
Building file: ../src/HelloOpenMP.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -fopenmp -MMD -MP -MF"src/HelloOpenMP.d" -MT"src/HelloOpenMP.d" -o "src/HelloOpenMP.o" "../src/HelloOpenMP.c"
Finished building: ../src/HelloOpenMP.c
Building target: HelloOpenMP
Invoking: MacOS X C Linker
gcc -o "HelloOpenMP" ./src/HelloOpenMP.o
Undefined symbols for architecture x86_64:
"_GOMP_parallel_end", referenced from:
_main in HelloOpenMP.o
"_GOMP_parallel_start", referenced from:
_main in HelloOpenMP.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [HelloOpenMP] Error 1
So I guess this was not the right place to add the -fopenmp compiler option? What configuration should I use to build with OpenMP?
Add -fopenmp flag to the linker section as well.

Resources