Search code examples
cmacoscmakeclionjson-c

Undefined symbols for architecture x86_64 with Json-C


Basically it's my first stack overflow question, so I am sorry for any inaccuracies/stupid questions here + I have tried for 3 days to solve my problem using all other answers people gave on similar questions, but either way I don't know Cmake enough or I am doing something wrong from the very beginning.

I've installed Json-c through Homebrew (brew install json-c). I am using Clion on macOS which uses Cmake. I have modified my CmakeLists:

cmake_minimum_required(VERSION 3.10) 
project(Project C)

set(CMAKE_C_STANDARD 99) 
INCLUDE_DIRECTORIES(/usr/local/Cellar/json-c/0.13.1/include/json-c) 
LINK_DIRECTORIES(/usr/local/Cellar/json-c/0.13.1/lib)


add_executable(Project main.c functions.c functions.h exp_functions.c exp_functions.h) 
TARGET_LINK_LIBRARIES(Project)

but I am not sure how to set arguments for TARGET_LINK_LIBRARIES.

When I am trying to build the project it gives me this:

 Undefined symbols for architecture x86_64:


    "_json_object_new_double", referenced from:
          _GenerateJson in functions.c.o
      "_json_object_new_object", referenced from:
          _GenerateJson in functions.c.o
      "_json_object_new_string", referenced from:
          _GenerateJson in functions.c.o
      "_json_object_object_add", referenced from:
          _GenerateJson in functions.c.o
      "_json_object_to_json_string", referenced from:
          _GenerateJson in functions.c.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

From what I have found on the web, someone had similar problem (https://github.com/json-c/json-c/issues/235), but after I have installed the package through Brew, I don't have any "json-c" files/folders in /usr/local/lib. There's only "json-c" alias in usr/local/include to brew directory.

I have tried to install json-c using instructions that are provided on repository site (https://github.com/json-c/json-c), but after that I had no clue how to set CmakeLists.txt to make the project work

What am I doing wrong. I am fresh at programming at all + I don't know much of the cmake and how macOS/Homebrew manages files when I am installing new packages through git clone.


Solution

  • You are missing something like

    LDFLAGS+= -ljson-c
    

    to link with the library. It was in the setup instructions for CMake in their README.