0

I am writing a bash script with the purpose of creating a static library for C code and creating an executable. The C program itself works fine, the problem I'm having is that when I run my current script I get the following readout in terminal.

This script will create Lab3 executable and static library.
.//liblab3.a(sin.o): In function `funcSin':
sin.c:(.text+0x13): undefined reference to `sin'
collect2: error: ld returned 1 exit status
End of script

I am very new to bash so I am at a loss.

This is my current script.

#!/bin/bash

echo "This script will create Lab3 executable and static library."

#create object files
gcc -c lab3main.c -o lab3main.o
gcc -c sin.c -o sin.o
gcc -c sphere.c -o sphere.o
gcc -c sumFloats.c -o sumFloats.o
gcc -c volCylinder.c -o volCylinder.o

#create static library
ar rcs liblab3.a lab3main.o sin.o sphere.o sumFloats.o volCylinder.o

#statically link & create executable
gcc lab3main.o -L./ -llab3 -o lab3static

echo "End of script"
  • 1
    I doubt the C program works fine if you cannot even compile it. That is a compilation error, it's one of the `gcc` commands in the script complaining that it cannot find the `sin` function (probably referenced in `lab3main.o`). – Marco Bonelli Apr 20 '20 at 05:25
  • 3
    The standard C math functions (like e.g. [`sin`](https://en.cppreference.com/w/c/numeric/math/sin)) are in their own library that you need to link with. You need to add the `-lm` option when linking. – Some programmer dude Apr 20 '20 at 05:27
  • 1
    On another unrelated note, I suggest you try to learn some other build-system than shell-scripts. On POSIX systems (like Linux or macOS) the most common is `make`, but there are many other that can handle not only simple project management and build dependencies but also configuration and checking for features. Something like that will come in handy as your projects gets bigger and bigger. Especially the dependency handling will make a lot of sense (only rebuilding the source files that are changed). – Some programmer dude Apr 20 '20 at 05:30
  • I will try adding -lm – Jesse Schultz Apr 20 '20 at 05:31
  • Note that linking with the math library will make use of the *standard* `sin` function, not yours (if you made your own). Without a [mcve] (especially not knowing what's in your `sin.c` source file) we can't really post a usable answer without resorting to guessing. – Some programmer dude Apr 20 '20 at 05:33
  • 1
    @Someprogrammerdude I know. It is for a class lab. The professor wants us to be familiar with scripting. Later instructions in the same lab have us doing the build with a make – Jesse Schultz Apr 20 '20 at 05:33
  • Adding -lm did the trick. Thank you very much – Jesse Schultz Apr 20 '20 at 05:36

0 Answers0