Skip to content

Commit 0b8f90d

Browse files
committed
Add C-lib example to README_DEVELOPERS.md
1 parent 41a04b4 commit 0b8f90d

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

README_DEVELOPERS.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,70 @@ and run pytest from the `array-api-tests` source dir like this:
8080
``` bash
8181
ARRAY_API_TESTS_MODULE=blosc2 pytest array_api_tests --xfails-file ${BLOSC2_DIR}/tests/array-api-xfails.txt -xs
8282
```
83+
84+
# Using the C-library
85+
Since C-blosc2 is shipped as a compiled binary with python-blosc2, one can compile and run C code using C-blosc2 functions. As of python-blosc2 version 4.0, one can find the location of the ``include`` files and binaries as follows. Run the following command in the terminal, which will give as output the path to the ``__init__.py`` file within the blosc2 folder.
86+
```bash
87+
python -c "import blosc2; print(blosc2.__file__)"
88+
path/to/blosc2/__init__.py
89+
```
90+
## Using CMake
91+
One may then access the include files via ``path/to/blosc2/include`` and the binaries via ``path/to/blosc2/lib``. Thus one may link a C-app via a ``CMakelists.txt`` file with the following snippet
92+
```
93+
# Add directory to search list for find_package
94+
set(CMAKE_PREFIX_PATH "$(python - <<EOF
95+
import blosc2, pathlib
96+
print(pathlib.Path(blosc2.__file__).parent)
97+
EOF)")
98+
99+
find_package(Blosc2 CONFIG REQUIRED)
100+
101+
target_link_libraries(myapp PRIVATE Blosc2::blosc2_shared)
102+
```
103+
104+
## Using pkg-config
105+
If one prefers to avoid using CMake, one can also use the pkg-config that is shipped with the wheel by running the following sequence of commands
106+
107+
First
108+
```
109+
BLOSC2_PREFIX=$(python - <<'EOF'
110+
import blosc2, pathlib
111+
print(pathlib.Path(blosc2.__file__).parent)
112+
EOF
113+
)\
114+
export PKG_CONFIG_PATH="$BLOSC2_PREFIX/lib/pkgconfig"
115+
```
116+
117+
We can check that the .pc file has the required info and has been found via
118+
``bash
119+
pkg-config --modversion blosc2
120+
``
121+
122+
Then define a test program
123+
```bash
124+
cat > test.c <<'EOF'
125+
#include <stdio.h>
126+
#include <blosc2.h>
127+
128+
int main(void) {
129+
printf(blosc2_get_version_string());
130+
return 0;
131+
}
132+
EOF
133+
```
134+
and compile it to an executable
135+
```bash
136+
gcc test.c \
137+
$(pkg-config --cflags --libs blosc2) \
138+
-Wl,--enable-new-dtags \
139+
-Wl,-rpath,"\$ORIGIN" \
140+
-o test_blosc2
141+
```
142+
The executable has to have access to the C library, so we copy the shared library to the executable directory
143+
```bash
144+
cp "$BLOSC2_PREFIX/lib/"libblosc2.so .
145+
```
146+
and run the executable
147+
```bash
148+
./test_blosc2
149+
```

0 commit comments

Comments
 (0)