Skip to content

Commit

Permalink
Fixed binding/linkiing (#2)
Browse files Browse the repository at this point in the history
* tmp

* fixed linking, binding module name

* update readme
  • Loading branch information
kaihsin authored Dec 3, 2024
1 parent e078431 commit a24694b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/include/
COMPONENT headers
FILES_MATCHING PATTERN "*.h*")


export(EXPORT cytnx_targets FILE ${CMAKE_CURRENT_BINARY_DIR}/CytnxTargets.cmake NAMESPACE cytnx_core::)
export(PACKAGE cytnx_core)

Expand All @@ -80,8 +81,13 @@ export(PACKAGE cytnx_core)
set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)

pybind11_add_module(pycytnx MODULE src/cpp/pybind/main.cpp)
target_link_libraries(pycytnx PUBLIC cytnx_core)
pybind11_add_module(_core MODULE src/cpp/pybind/main.cpp)
target_link_libraries(_core PUBLIC cytnx_core)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/cytnx_core
DESTINATION ${CMAKE_INSTALL_PREFIX}
)
install(TARGETS _core LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core)
#install(TARGETS pycytnx DESTINATION ${SKBUILD_PROJECT_NAME})
install(TARGETS pycytnx DESTINATION ${CMAKE_INSTALL_PREFIX}/cytnx_core)
message(STATUS " skbuild Installation Prefix: ${SKBUILD_PROJECT_NAME}")
message(STATUS " skbuild Installation Prefix: ${SKBUILD_HEADERS_DIR}")
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ Running pytest:
- cmake 3.15+ (see CMakeList.txt, default 3.20)

* most of the deps should be able to install via pypi.


## Compile directly the C++ package

```bash
$mkdir build
$cd build
$cmake ../ -DCMAKE_INSTALL_PREFIX=<install destination>
```
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = []
[tool.scikit-build]
minimum-version = "build-system.requires"
build-dir = "build/{wheel_tag}"
build.tool-args = ["-j4"]

[tool.black]
line-length = 88
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <cstring>
#include <string>

namespace cytnx_core {

void test();
std::string test();

}
6 changes: 4 additions & 2 deletions src/cpp/pybind/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <cytnx_core/test.hpp>
#include <pybind11/pybind11.h>
#include <string>

std::string hello_from_bin() { return "Hello from Kai!"; }
std::string hello_from_bin() { return cytnx_core::test(); }

namespace py = pybind11;

PYBIND11_MODULE(pycytnx, m) {
PYBIND11_MODULE(_core, m) {
m.doc() = "pybind11 hello module";

m.def("hello_from_bin", &hello_from_bin, R"pbdoc(
Expand Down
1 change: 0 additions & 1 deletion src/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
target_sources_local(cytnx_core
PRIVATE

# put cpp files here
test.cpp

Expand Down
7 changes: 5 additions & 2 deletions src/cpp/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "test.hpp"
#include <cytnx_core/test.hpp>
#include <iostream>
#include <string>

void test() { std::cout << "Hello from test" << std::endl; }
namespace cytnx_core {
std::string test() { return "Hello from test"; }
} // namespace cytnx_core
2 changes: 1 addition & 1 deletion src/cytnx_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cytnx_core.pycytnx import hello_from_bin
from cytnx_core._core import hello_from_bin


def hello() -> str:
Expand Down

0 comments on commit a24694b

Please sign in to comment.