Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
链接JSON-C静态库的编译器选项缺少什么?_C_Static Libraries_Json C - Fatal编程技术网

链接JSON-C静态库的编译器选项缺少什么?

链接JSON-C静态库的编译器选项缺少什么?,c,static-libraries,json-c,C,Static Libraries,Json C,我试图编译json-c-0.9测试二进制文件,同时静态链接到libjson.a,我已经构建了它,它位于/path/to/json-c-0.9/lib: $ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib -ljson test1.c -o test1 我发现表格中有很多错误: /path/to/json-c-0.9/tes

我试图编译
json-c-0.9
测试二进制文件,同时静态链接到
libjson.a
,我已经构建了它,它位于
/path/to/json-c-0.9/lib

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib -ljson test1.c -o test1                                                  
我发现表格中有很多错误:

/path/to/json-c-0.9/test1.c:17: undefined reference to `json_object_new_string'                                                        
/path/to/json-c-0.9/test1.c:18: undefined reference to `json_object_get_string'                                                        
/path/to/json-c-0.9/test1.c:19: undefined reference to `json_object_to_json_string'                                                    
/path/to/json-c-0.9/test1.c:20: undefined reference to `json_object_put'                                                               
/path/to/json-c-0.9/test1.c:22: undefined reference to `json_object_new_string'
etc.

在编译测试二进制文件时,我缺少了什么?感谢您的建议。

对于静态链接,gcc只会根据已经遇到的问题尝试引入所需的符号。在您的例子中,您在源文件之前传递
-ljson
,因此gcc引入了静态库,不需要任何东西,然后尝试构建代码

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib test1.c -o test1 -ljson
在代码之后放置要链接的库

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib test1.c -o test1 -ljson

这是我的Cmakelist.txt。untitle3,4是文件夹名称。请记住将头文件放在同一文件夹中或正确指向它

    cmake_minimum_required(VERSION 3.15)
project(untitled3 C )
#find_package( OpenCV REQUIRED )
ADD_LIBRARY(LibsModule
        main.c
        json.h
        libjson.c
        )

target_link_libraries(LibsModule -lpthread)
target_link_libraries(LibsModule libjson-c.a)
target_link_libraries(LibsModule libjson-c.4.dylib)

target_link_libraries(LibsModule -L/usr/local/Cellar/json-c/0.13.1/lib)

include_directories(/usr/local/lib/pkgconfig)
include_directories(untitled3)
set(CMAKE_C_STANDARD 99)
set(SOURCES  json.h main.c )

configure_file (
        "${PROJECT_SOURCE_DIR}/json.h"
        "${PROJECT_BINARY_DIR}/json.h"
)


add_executable( untitled4 ${SOURCES} )

target_link_libraries(untitled4 LibsModule)

你让我开心!谢谢