Search code examples
c++makefileclang++dylib

Creating dynamic library on Mac OSX using clang++


I wanted to learn how to make a shared library in C++. So I found some logic to generate Mandelbrot PPM images, and encapsulated it in src/mandelbrot.cpp (with accompanying header file include/mandelbrot.cpp). Directory tree looks like:

$ tree
.
├── Makefile
├── include
│   └── mandelbrot.h
├── lib
│   └── mandelbrot.dylib
└── src
    ├── client.cpp
    ├── main
    ├── main.cpp
    ├── mandelbrot.cpp
    └── mandelbrot.o

The goal is for src/client.cpp to use lib/mandelbrot.dylib to draw the fractal without having access to src/mandelbrot.cpp or src/mandelbrot.o.

Makefile:

.PHONY=clean

main: src/mandelbrot.o
    clang++ src/main.cpp -o src/main src/mandelbrot.o -I ./include

src/mandelbrot.o:
    clang++ -c src/mandelbrot.cpp -o src/mandelbrot.o -I ./include

clean:
    rm src/*.o
    rm lib/*.dylib

lib/mandelbrot.dylib:
    clang++ -dynamiclib -o lib/mandelbrot.dylib src/mandelbrot.cpp -I ./include

src/client: lib/mandelbrot.dylib
    clang++ src/client.cpp -o src/client -L ./lib -I ./include

Running the executable without the dylib works:

$ make main
clang++ src/main.cpp -o src/main src/mandelbrot.o -I ./include
$ ./src/main  # runs fine!

But I can't get my shared library to link when compiling src/client.cpp:

$ make src/client
clang++ src/client.cpp -o src/client -L ./lib -I ./include
Undefined symbols for architecture x86_64:
  "fractal::Mandelbrot::writeToFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in client-e344c7.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: *** [src/client] Error 1

Even though the symbol seems to be in the dylib:

$ file lib/mandelbrot.dylib 
lib/mandelbrot.dylib: Mach-O 64-bit dynamically linked shared library x86_64

$ nm -C lib/mandelbrot.dylib | grep writeToFile
0000000000001460 T fractal::Mandelbrot::writeToFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)

I'm trying to dig in deeper and understand the linking/compilation process. How do I make the setup I have work to demonstrate what I want to do?


Solution

  • Try naming your library like libmandelbrot.dylib to match the -lmandelbrot flag