Closed Muddasir-Shakil closed 9 months ago
If your library is not self contained but need functions from the underlying OS, you need to build your library as a zephyr library. I believe it's easier not to use the CMakeLists.txt from your library but construct a new one for Zephyr module.
diff --git a/include/libsample.h b/include/libsample.h
index 43e8333..885edba 100644
--- a/include/libsample.h
+++ b/include/libsample.h
@@ -1,3 +1,6 @@
#pragma once
#define LIBSAMPLE_VERSION "1.0"
+
+int sample_inet_pton(int af, const char *src, void *dst);
+int sample_inet_ntop(int af, const void *src, char *dst, socklen_t size);
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7e33c0a..42626e9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,3 +1,4 @@
target_sources(sample PRIVATE
plain.c
+ inet.c
)
diff --git a/src/inet.c b/src/inet.c
new file mode 100644
index 0000000..bbb5e28
--- /dev/null
+++ b/src/inet.c
@@ -0,0 +1,11 @@
+#include <arpa/inet.h>
+
+int sample_inet_pton(int af, const char *src, void *dst)
+{
+ return inet_pton(af, src, dst);
+}
+
+const char *sample_inet_ntop(int af, const void *src, char *dst, socklen_t size)
+{
+ return inet_ntop(af, src, dst, size);
+}
diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt
index b224377..83ac5f9 100644
--- a/zephyr/CMakeLists.txt
+++ b/zephyr/CMakeLists.txt
@@ -1,6 +1,11 @@
if(CONFIG_LIBSAMPLE)
- add_subdirectory(.. build)
- set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS sample)
- #zephyr_include_directories(../include)
- #zephyr_interface_library_named(sample)
+ zephyr_library()
+ zephyr_include_directories(${ZEPHYR_CURRENT_MODULE_DIR}/include)
+ if(CONFIG_POSIX_API)
+ zephyr_library_include_directories(${ZEPHYR_BASE}/include/zephyr/posix)
+ endif()
+ zephyr_library_sources(
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/plain.c
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/inet.c
+ )
endif()
diff --git a/src/main.c b/src/main.c
index 547ebe1..5df83f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,9 +1,17 @@
#include <zephyr/kernel.h>
+#include <arpa/inet.h>
#include <libsample.h>
void main(void)
{
+ struct in_addr addr;
+ char str[INET_ADDRSTRLEN];
+
printk("Hello libsample %s\n", LIBSAMPLE_VERSION);
+ sample_inet_pton(AF_INET, "192.168.1.1", &addr);
+ sample_inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
+ printk("%s\n", str);
+
return;
}
Thanks for confirming it @yashi. I was afraid that I may have to write additional CMakelist for it. I was trying to reuse the original CMakelist of the library and hoped that parent cmake would pass include flags to library. I have seen your approach in another similar project s2opc, unfortunately, they also had to rewrite cmake to integrate into zephye build system. I will try to create new one for zephyr integration. Thank you
I came up with this:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1526596..4f6238d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,7 @@ project(sample)
cmake_minimum_required(VERSION 3.20)
add_library(sample)
-target_compile_options(sample PRIVATE -Wall -Wextra)
+target_compile_options(sample PRIVATE -Wall)
set_target_properties(sample PROPERTIES C_STANDARD 11)
target_include_directories(sample PUBLIC include)
diff --git a/include/libsample.h b/include/libsample.h
index 43e8333..885edba 100644
--- a/include/libsample.h
+++ b/include/libsample.h
@@ -1,3 +1,6 @@
#pragma once
#define LIBSAMPLE_VERSION "1.0"
+
+int sample_inet_pton(int af, const char *src, void *dst);
+int sample_inet_ntop(int af, const void *src, char *dst, socklen_t size);
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7e33c0a..42626e9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,3 +1,4 @@
target_sources(sample PRIVATE
plain.c
+ inet.c
)
diff --git a/src/inet.c b/src/inet.c
new file mode 100644
index 0000000..bbb5e28
--- /dev/null
+++ b/src/inet.c
@@ -0,0 +1,11 @@
+#include <arpa/inet.h>
+
+int sample_inet_pton(int af, const char *src, void *dst)
+{
+ return inet_pton(af, src, dst);
+}
+
+const char *sample_inet_ntop(int af, const void *src, char *dst, socklen_t size)
+{
+ return inet_ntop(af, src, dst, size);
+}
diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt
index b224377..88c20a5 100644
--- a/zephyr/CMakeLists.txt
+++ b/zephyr/CMakeLists.txt
@@ -3,4 +3,6 @@ if(CONFIG_LIBSAMPLE)
set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS sample)
#zephyr_include_directories(../include)
#zephyr_interface_library_named(sample)
+ target_link_libraries(sample PRIVATE zephyr_interface)
+ target_link_libraries(sample PRIVATE posix_subsys)
endif()
It's hacky but seems to work in this case.
please re-open it if you have more questions.
first of thank you the detailed description.
I am trying to integrate an lib as module it uses POSIX BSD sockets. However, when compiling it always complains about missing headers, for example
zephyr/posix/arpa/inet.h: No such file or directory
. I wanted to ask how to pass the includes of zephyr to the modules at build time. Any help is appreciated. thank you. My prj.conf