最近在使用cmake对llama.cpp进行交叉编译,以生成linux可执行文件,需要在基于linux的、有安卓环境的设备上运行。之前对于2024年4月30日的llama.cpp版本(GitHub - ggerganov/llama.cpp: LLM inference in C/C++),使用的ndk版本为android-ndk-r20b-windows-x86_64,编译均能正常进行。
今天将本地llama.cpp更新为5月27日的版本后,按往常方法编译时遇到如下报错(摘取部分):
[ 0%] Building C object CMakeFiles/ggml.dir/ggml.c.o
D:/llama/llama.cpp/ggml.c:2869:5: error: unknown type name 'cpu_set_t'
cpu_set_t cpuset; // cpuset from numactl
^
D:/llama/llama.cpp/ggml.c:2907:8: error: unknown type name 'cpu_set_t'
static cpu_set_t ggml_get_numa_affinity(void) {
^
D:/llama/llama.cpp/ggml.c:2908:5: error: use of undeclared identifier 'cpu_set_t'
cpu_set_t cpuset;
D:/llama/llama.cpp/ggml.c:2912:5: error: implicit declaration of function
'pthread_getaffinity_np' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
D:/llama/llama.cpp/ggml.c:18964:22: error: implicit declaration of function
'CPU_ALLOC_SIZE' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
D:/llama/llama.cpp/ggml.c:18989:5: error: implicit declaration of function
'CPU_ZERO_S' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
CPU_ZERO_S(setsize, cpus);
出现上述问题后,尝试了github上类似问题的解决方法,尝试了GPT及KIMI的解决建议,例如自行定义cpu_set_t类型、或是对ggml.c的头文件删删改改,都是相同的报错。还尝试把所有涉及"cpu_set_t"的代码都注释掉,但是这样即使编译通过,生成的可执行文件也无法运行。
尝试了其他adb版本,包括r22、r26,都是相同报错。最后试到r21的时候发现编译通过了(android-ndk-r21e-windows-x86_64),生成的可执行文件也可以在终端上正常运行。