# linking Errors



## 2trill2spill (Jun 19, 2014)

I have a library I created called BlackBoxUtilities.so and when I go to link it against a executable I'm trying to compile, the linker can't find it. Here's what I've tried.

The project directory is /usr/home/nahnig/BlackBox.

`cc -I/usr/local/include -L/usr/home/nahnig/BlackBox -lBlackBoxUtilities.so fuzzer.c`

When I run that it says 

```
/usr/bin/ld: cannot find -lBlackBoxUtilities.so
cc: error: linker command failed with exit code 1 (use -v to see invocation)
```

So I tried...
`cc -I/usr/local/include -L/usr/home/nahnig/BlackBox -lBlackBoxUtilities fuzzer.c`
`cc -I/usr/local/include -L/usr/home/nahnig/BlackBox -o fuzzer fuzzer.c -lBlackBoxUtilities`

Then I copied the shared library to /usr/local/lib and tried `cc -I/usr/local/include -L/usr/local/lib -o fuzzer fuzzer.c -lBlackBoxUtilities`.

So I copied the library to /usr/lib thinking that's where ld() looks first. And tried `cc -I/usr/local/include -L/usr/lib -o fuzzer fuzzer.c -lBlackBoxUtilities`

Nothing works so I change directory to /usr/lib and `cc -I/usr/local/include -lBlackBoxUtilities fuzzer.` worked if I copied in the fuzzer executable.

So what am I doing wrong here?


----------



## freethread (Jun 20, 2014)

Rename the library to libBlackBoxUtilities.so, put it wherever you want and link against it with the option -lBlackBoxUtilities (also use the -L option if not in default library paths). Both GCC and CLANG expect library files prefixed with lib.


----------



## kpa (Jun 20, 2014)

In FreeBSD the /usr/local/lib directory is not searched by default for link libraries. This is to guarantee that the linker will only use link libraries from the base system unless otherwise instructed with -L options.


----------



## 2trill2spill (Jun 23, 2014)

I renamed the library so it has lib in front but now I am getting undefined references.


```
$ cc -03 -I/usr/local/include -o fuzzer fuzzer.c -LlibBlackBox.so
cc: warning: argument unused during compilation: '-03'
fuzzer.c:35:12: warning: implicit declaration of function 'parseInput' is
      invalid in C99 [-Wimplicit-function-declaration]
    rtrn = parseInput(input);
           ^
1 warning generated.
/tmp/fuzzer-7OhMPq.o: In function `main':
fuzzer.c:(.text+0x24): undefined reference to `helpScreen'
fuzzer.c:(.text+0x5a): undefined reference to `reallocarray'
fuzzer.c:(.text+0x7e): undefined reference to `errorHandler'
fuzzer.c:(.text+0xcb): undefined reference to `errorHandler'
fuzzer.c:(.text+0x112): undefined reference to `errorHandler'
fuzzer.c:(.text+0x12c): undefined reference to `parseInput'
fuzzer.c:(.text+0x18d): undefined reference to `getList'
fuzzer.c:(.text+0x1c8): undefined reference to `getLines'
fuzzer.c:(.text+0x207): undefined reference to `getFile'
fuzzer.c:(.text+0x266): undefined reference to `errorHandler'
cc: error: linker command failed with exit code 1 (use -v to see invocation)
```


----------



## ljboiler (Jun 23, 2014)

The proper syntax for linking to some libMyLib.so library that is in the /usr/local/lib directory is `cc -L/usr/local/lib -lMyLib <other options and/or source files>`. The -L option is used to name the directory where the library or libraries are located, and the -l option names the library to use, leaving off the "lib" part of the name in front and without the ".so" on the end.


----------



## 2trill2spill (Jun 23, 2014)

Sorry I should have been more specific. I am in the current directory (not /usr/include) as libBlackBox.so, the header libBlackBox.h and the main executable fuzzer.c.

libBlackBox.so is built from many object files using this Makefile:


```
CC=clang
CFLAGS= -c -Wall -Werror -o3 -I/usr/local/include -fpic
RM = rm -f
LDFLAGS= -shared -I/usr/local/include -fblocks -o3 -fpic
SOURCES= BlackBoxCaseGen.c BlackBoxCrypto.c BlackBoxError.c BlackBoxFuzzer.c BlackBoxNetworking.c BlackBoxObj.c BlackBoxParser.c BlackBoxUtilities.c RC6.c sha3.c twoFish.c
OBJECTS=$(SOURCES:.clang=.o)
TARGET_LIB = libBlackBox.so

.PHONY: all
all: ${TARGET_LIB}

$(TARGET_LIB): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
    
$(SRCS:.c=.d):%.d:%.c
$(CC) $(CFLAGS) -MM $< >$@
    
include $(SRCS:.c=.d)
    
.PHONY: clean
clean:
-${RM} ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)
```

Here's the header libBlackBox.h


```
#ifndef libBlackBox_h
#define libBlackBox_h

/* One Header to rule them all.  */
#include "BlackBoxFuzzer.h"
#include "BlackBoxUtilities.h"
#include "BlackBoxCaseGen.h"
#include "BlackBoxCrypto.h"
#include "BlackBoxError.h"
#include "BlackBoxParser.h"
#include "BlackBoxNetworking.h"

#endif
```

I'm not sure what's happening as this code compiles on Mac OSX just fine, but I*'*m new to FreeBSD so *I* must be doing something wrong. Copying the headers and libraries to /usr/include and /usr/lib didn't fix it either. I dropped the lib and .so off and now *I* get:


```
cc -o3 -I/usr/local/include -o fuzzer fuzzer.c -lBlackBox
fuzzer.c:35:12: warning: implicit declaration of function 'parseInput' is
      invalid in C99 [-Wimplicit-function-declaration]
    rtrn = parseInput(input);
           ^
1 warning generated.
/usr/lib/libBlackBox.so: undefined reference to `BN_bn2dec'
/usr/lib/libBlackBox.so: undefined reference to `avcodec_open2'
/usr/lib/libBlackBox.so: undefined reference to `avformat_find_stream_info'
/usr/lib/libBlackBox.so: undefined reference to `MD5_Init'
/usr/lib/libBlackBox.so: undefined reference to `_NSConcreteGlobalBlock'
/usr/lib/libBlackBox.so: undefined reference to `MD5_Final'
/usr/lib/libBlackBox.so: undefined reference to `BN_rand_range'
/usr/lib/libBlackBox.so: undefined reference to `BN_set_word'
/usr/lib/libBlackBox.so: undefined reference to `avcodec_find_decoder'
/usr/lib/libBlackBox.so: undefined reference to `_NSConcreteStackBlock'
/usr/lib/libBlackBox.so: undefined reference to `SHA256_Init'
/usr/lib/libBlackBox.so: undefined reference to `avformat_open_input'
/usr/lib/libBlackBox.so: undefined reference to `av_register_all'
/usr/lib/libBlackBox.so: undefined reference to `RAND_seed'
/usr/lib/libBlackBox.so: undefined reference to `dispatch_apply_f'
/usr/lib/libBlackBox.so: undefined reference to `BN_new'
/usr/lib/libBlackBox.so: undefined reference to `dispatch_get_global_queue'
/usr/lib/libBlackBox.so: undefined reference to `SHA256_Update'
/usr/lib/libBlackBox.so: undefined reference to `av_dump_format'
/usr/lib/libBlackBox.so: undefined reference to `MD5_Update'
/usr/lib/libBlackBox.so: undefined reference to `SHA256_Final'
/usr/lib/libBlackBox.so: undefined reference to `ERR_load_crypto_strings'
/usr/lib/libBlackBox.so: undefined reference to `BN_free'
/usr/lib/libBlackBox.so: undefined reference to `dispatch_sync'
cc: error: linker command failed with exit code 1 (use -v to see invocation)
```


----------



## ljboiler (Jun 23, 2014)

The errors show that your libBlackBox.so is being found, but that it uses functions in some other library or libraries. You must add to your original command 





> `cc -o3 -I/usr/local/include -o fuzzer fuzzer.c -lBlackBox`


 the extra library names where those functions named by the "undefined reference" errors can be found. The final command will look something like this: `cc -o3 -I/usr/local/include -o fuzzer fuzzer.c -lBlackBox -llibraryforavcodec -llibraryforMD5`. The exact names of those libraries will depend on what was #included in the BlackBox C code files.


----------



## 2trill2spill (Jun 23, 2014)

Ok, but how would I go about using one library? I thought if I compiled a library from multiple library object files I would be able to use just that one library. It kind of defeats the purpose of building that one library if I still have to link it to a bunch of other libraries any way.


----------



## ljboiler (Jun 23, 2014)

The whole idea behind dynamic libraries (.so files) is that they are smaller and don't include all the library object files from anything but your code.  You would have to change the libBlackBox makefile to statically link in all the library modules from whatever library or libraries that all those "undefined references" are actually in.


----------

