Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[C-API] Add extern to C API symbols to solve linker issue using C compiler (#28877)

Pawel Raasz56607df28b6a
diff --git a/samples/c/hello_classification/CMakeLists.txt b/samples/c/hello_classification/CMakeLists.txt
index 134ce3fe908..97e9cd6ac87 100644
--- a/samples/c/hello_classification/CMakeLists.txt
+++ b/samples/c/hello_classification/CMakeLists.txt
@@ -3,5 +3,6 @@
#
ov_add_sample(NAME hello_classification_c
- SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.c"
+ SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.c;${CMAKE_CURRENT_SOURCE_DIR}/infer_result_util.c"
+ HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/infer_result_util.h"
DEPENDENCIES opencv_c_wrapper)
diff --git a/samples/c/hello_classification/infer_result_util.c b/samples/c/hello_classification/infer_result_util.c
new file mode 100644
index 00000000000..21d75bdd304
--- /dev/null
+++ b/samples/c/hello_classification/infer_result_util.c
@@ -0,0 +1,61 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "infer_result_util.h"
+
+int compare(const void* a, const void* b) {
+ const struct infer_result* sa = (const struct infer_result*)a;
+ const struct infer_result* sb = (const struct infer_result*)b;
+ if (sa->probability < sb->probability) {
+ return 1;
+ } else if ((sa->probability == sb->probability) && (sa->class_id > sb->class_id)) {
+ return 1;
+ } else if (sa->probability > sb->probability) {
+ return -1;
+ }
+ return 0;
+}
+void infer_result_sort(struct infer_result* results, size_t result_size) {
+ qsort(results, result_size, sizeof(struct infer_result), compare);
+}
+
+struct infer_result* tensor_to_infer_result(ov_tensor_t* tensor, size_t* result_size) {
+ ov_shape_t output_shape = {0};
+ ov_status_e status = ov_tensor_get_shape(tensor, &output_shape);
+ if (status != OK)
+ return NULL;
+
+ *result_size = output_shape.dims[1];
+
+ struct infer_result* results = (struct infer_result*)malloc(sizeof(struct infer_result) * (*result_size));
+ if (!results)
+ return NULL;
+
+ void* data = NULL;
+ status = ov_tensor_data(tensor, &data);
+ if (status != OK) {
+ free(results);
+ return NULL;
+ }
+ float* float_data = (float*)(data);
+
+ size_t i;
+ for (i = 0; i < *result_size; ++i) {
+ results[i].class_id = i;
+ results[i].probability = float_data[i];
+ }
+
+ ov_shape_free(&output_shape);
+ return results;
+}
+
+void print_infer_result(struct infer_result* results, size_t result_size, const char* img_path) {
+ printf("\nImage %s\n", img_path);
+ printf("\nclassid probability\n");
+ printf("------- -----------\n");
+ size_t i;
+ for (i = 0; i < result_size; ++i) {
+ printf("%zu %f\n", results[i].class_id, results[i].probability);
+ }
+}
diff --git a/samples/c/hello_classification/infer_result_util.h b/samples/c/hello_classification/infer_result_util.h
new file mode 100644
index 00000000000..b704fc29572
--- /dev/null
+++ b/samples/c/hello_classification/infer_result_util.h
@@ -0,0 +1,43 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <openvino/c/openvino.h>
+#include <stdlib.h>
+
+/**
+ * @brief Struct to store infer results
+ */
+struct infer_result {
+ size_t class_id;
+ float probability;
+};
+
+/**
+ * @brief Sort result by probability
+ * @param struct with infer results to sort
+ * @param result_size of the struct
+ * @return none
+ */
+int compare(const void* a, const void* b);
+
+void infer_result_sort(struct infer_result* results, size_t result_size);
+
+/**
+ * @brief Convert output tensor to infer result struct for processing results
+ * @param tensor of output tensor
+ * @param result_size of the infer result
+ * @return struct infer_result
+ */
+struct infer_result* tensor_to_infer_result(ov_tensor_t* tensor, size_t* result_size);
+
+/**
+ * @brief Print results of infer
+ * @param results of the infer results
+ * @param result_size of the struct of classification results
+ * @param img_path image path
+ * @return none
+ */
+void print_infer_result(struct infer_result* results, size_t result_size, const char* img_path);
diff --git a/samples/c/hello_classification/main.c b/samples/c/hello_classification/main.c
index 8fc4ad4e8cd..6b44c29527d 100644
--- a/samples/c/hello_classification/main.c
+++ b/samples/c/hello_classification/main.c
@@ -8,91 +8,9 @@
#include <stdlib.h>
#include <string.h>
+#include "infer_result_util.h"
#include "openvino/c/openvino.h"
-/**
- * @brief Struct to store infer results
- */
-struct infer_result {
- size_t class_id;
- float probability;
-};
-
-/**
- * @brief Sort result by probability
- * @param struct with infer results to sort
- * @param result_size of the struct
- * @return none
- */
-int compare(const void* a, const void* b) {
- const struct infer_result* sa = (const struct infer_result*)a;
- const struct infer_result* sb = (const struct infer_result*)b;
- if (sa->probability < sb->probability) {
- return 1;
- } else if ((sa->probability == sb->probability) && (sa->class_id > sb->class_id)) {
- return 1;
- } else if (sa->probability > sb->probability) {
- return -1;
- }
- return 0;
-}
-void infer_result_sort(struct infer_result* results, size_t result_size) {
- qsort(results, result_size, sizeof(struct infer_result), compare);
-}
-
-/**
- * @brief Convert output tensor to infer result struct for processing results
- * @param tensor of output tensor
- * @param result_size of the infer result
- * @return struct infer_result
- */
-struct infer_result* tensor_to_infer_result(ov_tensor_t* tensor, size_t* result_size) {
- ov_shape_t output_shape = {0};
- ov_status_e status = ov_tensor_get_shape(tensor, &output_shape);
- if (status != OK)
- return NULL;
-
- *result_size = output_shape.dims[1];
-
- struct infer_result* results = (struct infer_result*)malloc(sizeof(struct infer_result) * (*result_size));
- if (!results)
- return NULL;
-
- void* data = NULL;
- status = ov_tensor_data(tensor, &data);
- if (status != OK) {
- free(results);
- return NULL;
- }
- float* float_data = (float*)(data);
-
- size_t i;
- for (i = 0; i < *result_size; ++i) {
- results[i].class_id = i;
- results[i].probability = float_data[i];
- }
-
- ov_shape_free(&output_shape);
- return results;
-}
-
-/**
- * @brief Print results of infer
- * @param results of the infer results
- * @param result_size of the struct of classification results
- * @param img_path image path
- * @return none
- */
-void print_infer_result(struct infer_result* results, size_t result_size, const char* img_path) {
- printf("\nImage %s\n", img_path);
- printf("\nclassid probability\n");
- printf("------- -----------\n");
- size_t i;
- for (i = 0; i < result_size; ++i) {
- printf("%zu %f\n", results[i].class_id, results[i].probability);
- }
-}
-
void print_model_input_output_info(ov_model_t* model) {
char* friendly_name = NULL;
ov_model_get_friendly_name(model, &friendly_name);
diff --git a/src/bindings/c/include/openvino/c/ov_common.h b/src/bindings/c/include/openvino/c/ov_common.h
index 12ad8318119..0ff6d39cd9e 100644
--- a/src/bindings/c/include/openvino/c/ov_common.h
+++ b/src/bindings/c/include/openvino/c/ov_common.h
@@ -33,7 +33,7 @@
#ifdef __cplusplus
# define OPENVINO_C_API_EXTERN extern "C"
#else
-# define OPENVINO_C_API_EXTERN
+# define OPENVINO_C_API_EXTERN extern
#endif
#if defined(OPENVINO_STATIC_LIBRARY) || defined(__GNUC__) && (__GNUC__ < 4)