6

I am trying to compile the json-c-0.9 test binaries, while statically linking to libjson.a, which I have built and is sitting in /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                                                  

I get numerous errors of the form:

/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.

What I am missing in trying to compile the test binaries? Thanks for your advice.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345

2 Answers2

10

With static linking, gcc only tries to bring in the symbols it needs based on what it has encountered already. In your case, you pass -ljson before your source files, so gcc brings in the static library and doesn't need anything from it, then tries to build your code.

Put the libraries to link against after your code.

$ gcc -g -v -Wall -std=gnu99 -static -L/path/to/json-c-0.9/lib test1.c -o test1 -ljson
wkl
  • 77,184
  • 16
  • 165
  • 176
0

Here's mine Cmakelist.txt. untitled3,4 are the folder names. Remember to put header file in the same folder or direct to it correctly.

    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)
Haseeb Saeed
  • 599
  • 2
  • 4
  • 19