Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] warning: linker input unused
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20484

PostPosted: Wed Sep 11, 2024 6:02 pm    Post subject: [solved] warning: linker input unused Reply with quote

I'm trying to learn how to use Makefiles with linking an external library as well as creating objects, executables and the C "extern" feature. This is sort of a "minimal" example of doing that.

Everything seems to work, although the linker warning doesn't help me understand what the warning means, or what to correct to stop the warning.
Code:
$ make
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -c common.c  -lsodium -o obj/common.o
clang: warning: -lsodium: 'linker' input unused [-Wunused-command-line-argument]
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -c foo.c  -lsodium -o obj/foo.o
clang: warning: -lsodium: 'linker' input unused [-Wunused-command-line-argument]
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -o foo  obj/common.o  obj/foo.o -lsodium

$ ./foo
Random number: 8
If I remove -lsodium from the Makefile, it produces this error:
Code:
undefined reference to `randombytes_uniform'
What should I be doing instead?
Code:
$ cat Makefile
CC=/usr/lib/llvm/18/bin/clang
STANDARD=c11

CFLAGS= -Wall -Wextra -std=$(STANDARD) -pedantic -ggdb

LIBS= -lsodium
SRC=foo.c

SOURCES=$(wildcard ./*.c)
OBJECTS=$(patsubst ./%.c, obj/%.o, $(SOURCES))

EXECUTABLE=foo

all:    $(EXECUTABLE)

$(EXECUTABLE):  $(OBJECTS)
        $(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LIBS)

$(OBJECTS): obj/%.o : ./%.c
        $(CC) $(CFLAGS) -c $< $(LIB_PATH) $(LIBS) -o $@
Code:
$ head -15 common.c
#include <sodium.h>
#include <stdbool.h>

unsigned int get_random_number(int max)
{
    if (sodium_init() < 0) {
        /* Unable to initialize sodium (unsafe, to use) */
        return 1;
    }

    // randombytes_uniform() returns a value: 0 to upper bound (excluded).
    int number = (randombytes_uniform(max) + 1);

    return number;
}
// Not shown or used (yet) are functions for is_... odd, even. true, false

$ cat foo.c
#include <stdio.h>
#include "foo.h"

int main(void)
{
    printf("Random number: %d\n", get_random_number(10));
}

$ cat foo.h
extern int get_random_number(int number);

_________________
Quis separabit? Quo animo?


Last edited by pjp on Wed Sep 11, 2024 6:59 pm; edited 1 time in total
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1685
Location: South America

PostPosted: Wed Sep 11, 2024 6:19 pm    Post subject: Reply with quote

LIBS is only needed in the linking phase, i. e. the recipe for $(EXECUTABLE).
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20484

PostPosted: Wed Sep 11, 2024 6:57 pm    Post subject: Reply with quote

Thanks.
Code:
$ make
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -c common.c  -o obj/common.o
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -c foo.c  -o obj/foo.o
/usr/lib/llvm/18/bin/clang -Wall -Wextra -std=c11 -pedantic -ggdb -o foo  obj/common.o  obj/foo.o -lsodium


As far as the (now deleted) comment about make figuring out how to create object files, that's not something I understand at all. It's a Franken-Makefile I've cobbled together here and there as I've tried to figure out how to do something.

This stuff is a precursor to using multiple directories for source files and how to get them to work together. I tried using src/*.c, that worked. I then added src/sub-dir/*.c, and that didn't work. Next I tried src/*.c and other-src/*.c and couldn't get the two to work.

So for now I'm back with everything in src/*.c
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1685
Location: South America

PostPosted: Wed Sep 11, 2024 8:11 pm    Post subject: Reply with quote

pjp wrote:
As far as the (now deleted) comment about make figuring out how to create object files, that's not something I understand at all.

I saw that you wanted the object files in an obj/ subdirectory, so I deleted the comment.

If you don't mind C source files, object files and the executable in the same directory as the makefile, you can omit the rule for $(OBJECTS), because GNU Make can deduce that it can make common.o and foo.o from their source files by invoking a C compiler, and will use your specified CC and CFLAGS for that. It is using an implicit rule; see the relevant section in info make. That makes the makefile shorter.

If you don't mind object files and the executable in the same directory as the makefile, but have all C source files somewhere else, you can also omit that rule, and add VPATH=/pathname/of/sources/directory (it can be relative to the makefile's directory), so that the source files are searched there in addition to the makefile's directory.

Other variants require a more complicated makefile.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum