1

I am trying to add options to the make command. For example to add -l option to include libraries. Something like clang filename.c -o filename to clang filename.c -o filename -l{libary}

What should I do and what file should I edit?

7
  • Does the project you are trying to build actually have a makefile? or are you typing the clang command you showed manually? Dec 20, 2017 at 13:17
  • typing make runs clang filename.c -o filename i just want to add options to it so that it runs for example clang filename.c -o filename -l
    – Dokeisoi
    Dec 20, 2017 at 13:19
  • OK but does that command come from an actual Makefile in the current directory, or because you have set the default CC to clang? Dec 20, 2017 at 13:21
  • yeah i changed the default CC to clang
    – Dokeisoi
    Dec 20, 2017 at 13:31
  • 1
    You still haven't told us if the project has a Makefile or not.
    – muru
    Dec 20, 2017 at 14:05

4 Answers 4

4

Since you don't have a Makefile, compilation and linking of your program relies on implicit rules built in to make. In the case of C source code, the implicit rule for Linking a single object file is:

$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS) 

So you can add libraries via the LDLIBS variable e.g.

make LDLIBS='-lfoo'

or

LDLIBS='-lfoo' make

See GNU make: 10.2 Catalogue of Built-In Rules

2
  • where do i put these?
    – Dokeisoi
    Dec 20, 2017 at 14:09
  • 1
    @Satori you can add them to the make command line - exactly as I wrote. Or if you're too lazy to type them, make an alias alias make='make "LDLIBS=-lm"' but I don't recommend it - once you start modifying build commands, it's better to create a minimal Makefile for your program. Dec 20, 2017 at 15:00
1

According to fedorqui, an alias cannot accept a parameter. Hence, you must create a function in ~/.bashrc.

acp ()
{
clang "$1".c -o "$1" -l
}

Execute with

acp "filename"

It's said that it is important to use double quotes to avoid just getting the first parameter, i.e. if there's a space in the filename.

Note that I did not test this.

Reference: User Input to Bash Alias?

0

If I understood you right, the simplest thing would be to make an alias in your .bashrc or .zshrc or whatever shell you are using.

I would not recommend you to edit something somewhere else.

But you could look where you find the program, some programs are scripts, written in perl for example, and can be edited to your will

3
  • 1
    how should i alias it because the filename is going to be different each time?
    – Dokeisoi
    Dec 20, 2017 at 13:17
  • You can work with parameters of this helps. I don't really see the bigger picture of your situation, pls elaborate
    – user773600
    Dec 20, 2017 at 13:21
  • 1
    I'm just trying to include a library when i compile the code, but the make command doesn't have the -l command and runs clang filename.c -o filename so i want to include the -l in the make command so every time i use it runs clang filename.c -o filename -l
    – Dokeisoi
    Dec 20, 2017 at 13:36
0

I used export LDLIBS='-l' in my .bashrc file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .