dylib and how to use it

I want to use an external library I downloaded online. In the downloaded folder I have the following files:
- libexternal.dylib
- libexternal.h
- libexternal.hpp

My system is macOS Catalina, and I'm not sure how to include this dynamic library in my programm. I've found following possibilities online:
1) use CMake (find_package(), include_directories()...)
2) put *.dylib directly under the path like /usr/Xxx/lib/

So far none of them work and I keep getting this error:

1
2
ld: library not found for -laddliblibrary
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Has someone experienced this? Every input is appreciated!
Maybe it would help if you posted exactly the names of the files, where you're putting them, and how you're trying to use them. It might just be that you're making a silly typo or something.
There are a couple of ways you can include it in your program.

1. If you move the header files to the same directory that your program is in, then you can use
 
#include "whatever" 

2. You can move them to the /usr/include directory. That is a hidden directory where all of your header files like <cstdlib>, <cstdio>, <iostream>, etc are stored.
If you put them in there, then you can use
 
#include <whatever> 

Either one will work. And, if you use the second one, you can include it in any C++ program anywhere on your computer. If you use the first, then you can only include it in programs in that directory.

Good luck!
max
Last edited on
hi thanks for replying.

I'm trying to use DuckDB: https://duckdb.org/docs/api/cpp
I downloaded the file here: https://duckdb.org/docs/installation/?environment=cplusplus

the structure of the downloaded zip is:
- libduckdb.dylib
- duckdb.hpp
- duchdb.h
I think what @helios meant is where did you store the files on your computer after downloading them? Because the location is important if you're going to include them in your programs.
hi max

i stored the *.dylib file inside my project as the follows:
1
2
3
4
5
6
7
8
9
duckdb-test
- cmake-build-debug/
- CMakeLists.txt
- main.cpp
- dependencies/
	- libduckdb-osx-amd64
		- duckdb.h
		- duckdb.hpp
		- libduckdb.dylib		


I managed to #include "duckdb.hpp" in main.cpp file. But somehow it looks like it can only find the header file, not the real implementation(the function body for instance)
This is how I wrote my CMakeLists.txt:
1
2
3
4
5
6
7
8
9
10
11
cmake_minimum_required(VERSION 3.17)
project(duckdb-test)

set(CMAKE_CXX_STANDARD 17)

include_directories(${CMAKE_SOURCE_DIR}/dependencies/libduckdb-osx-amd64) # *.h
link_directories(${CMAKE_SOURCE_DIR}/dependencies/libduckdb-osx-amd64)    # *.dylib

add_executable(duckdb-test main.cpp)

target_link_libraries(duckdb-test libduckdb.dylib)
What compiler are you using? If you're using Visual Studio or Xcode, it should include the implementation automatically, if it's in the same directory.

Try using the Terminal application or the command line (if you aren't already), and use this template command:

c++ -std=c++2a -g -o exec main.cpp implementation.cpp
OR
g++ -std=c++2a -g -o exec main.cpp implementation.cpp


c++: The clang++ compiler for compiling C++ programs.
g++: The GNU++ compiler for C++ programs.
-std=c++2a: Tells the compiler to use the C++20 standard. You can also try c++2b instead of c++2a for a more recent version.
-g: Tells the compiler to create a special file for debugging; if you aren't planning on debugging it, you can omit this flag.
-o exec: Tells the compiler to name the executable "exec". If you omit this flag, it will default to "a.out".
main.cpp: Your driver program (main).
implementation.cpp: Your class implementation.

There are various other flags you can insert, like -Wall, -Wextra, and -pedantic, those are for enabling warnings that wuldn't normally be shown.
hi max,

thanks for your feedback.

I finally got it work by modifying the CMakeLists.txt
from
 
target_link_libraries(duckdb-test libduckdb.dylib)

to
 
target_link_libraries(duckdb-test duckdb)

javascript:editbox1.editPreview()
Ah, ok. Well that's good!
Registered users can post here. Sign in or register to post.