Issue
I'm trying to use the kubernetes c client api library[https://github.com/kubernetes-client/c/].
I just created a CMake C++ project. The project contain only CMakeLists.txt
and main.cpp
files. I have followed the instructions to install the client library and libkubernetes.so
is found on /usr/local/lib
Following is my code.
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(k8s)
set(CMAKE_CXX_STANDARD 17)
add_executable(k8s main.cpp)
target_link_libraries(k8s PRIVATE /usr/local/lib/libkubernetes.so)
main.cpp
#include <iostream>
#include <kubernetes/include/apiClient.h>
#include <kubernetes/config/kube_config.h>
#include <kubernetes/api/CoreV1API.h>
int main() {
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL;
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL);/* NULL means loading configuration from $HOME/.kube/config */
if (rc != 0) {
printf("Cannot load kubernetes configuration.\n");
return -1;
}
// commented below for [check 1]
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (!apiClient) {
printf("Cannot create a kubernetes client.\n");
return -1;
}
v1_pod_list_t *pod_list = NULL;
pod_list = CoreV1API_listNamespacedPod(apiClient,
"default", /*namespace */
NULL, /* pretty */
NULL, /* allowWatchBookmarks */
NULL, /* continue */
NULL, /* fieldSelector */
NULL, /* labelSelector */
NULL, /* limit */
NULL, /* resourceVersion */
NULL, /* resourceVersionMatch */
NULL, /* sendInitialEvents */
NULL, /* timeoutSeconds */
NULL /* watch */
);
printf("return code=%ld\n", apiClient->response_code);
apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();
}
When I build and compiled with cmake .
and make
I got following error.
/usr/bin/ld: CMakeFiles/k8s.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x81): undefined reference to `apiClient_create_with_base_path(char const*, sslConfig_t*, list_t*)'
/usr/bin/ld: main.cpp:(.text+0xe8): undefined reference to `CoreV1API_listNamespacedPod(apiClient_t*, char*, char*, int*, char*, char*, char*, int*, char*, char*, int*, int*, int*)'
/usr/bin/ld: main.cpp:(.text+0x11b): undefined reference to `apiClient_free(apiClient_t*)'
/usr/bin/ld: main.cpp:(.text+0x157): undefined reference to `apiClient_unsetupGlobalEnv()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/k8s.dir/build.make:98: k8s] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/k8s.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
[check 1] Since the error is not reported for load_kube_config
I just check commenting the shown part.
Then, when I built the project with cmake .
and make
got following error.
error while loading shared libraries: libyaml.so: cannot open shared object file: No such file or directory
But, I managed to get it fixed by exporting LD_LIBRARY_PATH=/usr/local/lib
. The the code part with load_kube_config
works.
Then, I uncommented code part I commented before executed. I still get the same error.
I just wondering If I linked libkubernetes.so
and all of those api interface files are coming from that library why some of the functions do work and some don't.
What is the problem and how can I fix it?
Solution
They seem don't care of C++ and including their header files into .cpp files. Since your code is the plain C if you remove #include <iostream>
, rename your file main.cpp to main.c. Or include their files forcing C linkages for their API functions:
extern "C" {
#include <kubernetes/include/apiClient.h>
#include <kubernetes/api/CoreV1API.h>
}
#include <kubernetes/config/kube_config.h>
For refernce:
kubernetes/include/apiClient.h and kubernetes/api/CoreV1API.h do not have it, but
kubernetes/config/kube_config.h:7 has extern "C"
.
Answered By - 273K Answer Checked By - David Marino (WPSolving Volunteer)