Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined reference to `cnpy::npy_load(std::string)' #95

Open
rsmath opened this issue Sep 28, 2023 · 3 comments
Open

Undefined reference to `cnpy::npy_load(std::string)' #95

rsmath opened this issue Sep 28, 2023 · 3 comments

Comments

@rsmath
Copy link

rsmath commented Sep 28, 2023

Cnpy is having trouble when used in conjunction with PyTorch. I boiled down the problem to having included PyTorch in the CMakeLists.txt as follows

FIND_PACKAGE(Torch REQUIRED)
FIND_PACKAGE(ZLIB REQUIRED)
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})

INCLUDE_DIRECTORIES("/home/USER/cnpy")
LINK_DIRECTORIES("/home/USER/cnpy/build")

add_executable(example1 example1.cpp)
target_link_libraries(example1 cnpy)
TARGET_LINK_LIBRARIES(example1 "${TORCH_LIBRARIES}")
TARGET_LINK_LIBRARIES(example1 ${ZLIB_LIBRARIES})

When I do not enforce PyTorch to be found, the example code in the repository runs fine with loading cnpy. Otherwise, I get the following error.

image

@OolongBlender
Copy link

I think I have the same problem with you. It seems that there is conflict between libtorch and cnpy.
If I link libtorch and cnpy simultaneously to a target, I can't find cnpy using ldd command, which means cnpy doesn't be linked successfully.
If you have figure out this problem or have other useful information, I would really appreciate it!

@aboy2018
Copy link

aboy2018 commented Apr 7, 2024

I get the same problem #

@OolongBlender
Copy link

The problem might have been caused by using an incorrect version of LibTorch during installation.
pytorch versions
At first I installed the pre-cxx ABI version, I encountered linking failures with cnpy when trying to link both cnpy and torch to the target. You can use the ldd command to check the dependencies of the generated target.
ldd example1
Afterward, I reinstalled the cxx11 ABI version of LibTorch, and the problem was resolved.
The difference between cxx11 ABI and pre-cxx ABI

In itself, the choice would not matter too much (I’d probably take the cxx11 ABI).
The tricky part is when combining it with other libraries:
Typically C++ libraries are linked with the cxx11 ABI by default.
The official PyTorch Python module are linked with the old pre-cxx11 ABI, so if you want to use them, you need this.
If you have a mix of these, you are in a real pickle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants