Undefined symbols for architecture x86_64 with Json-C - 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.

You are missing something like
LDFLAGS+= -ljson-c
to link with the library. It was in the setup instructions for CMake in their README.

Related

Compiling a C program that uses OpenGl 4 in Mac OS X

So I am learning OpenGL 4 from openglbook.
I copied and pasted the simple window creating program in my editor and followed Compiling OpenGL programs on OS X by using
gcc -o hello hello.c -framework OpenGL -framework GLUT
I got the following error:
Undefined symbols for architecture x86_64:
"_glewGetErrorString", referenced from:
_Initialize in chapter-c8ba2d.o
"_glewInit", referenced from:
_Initialize in chapter-c8ba2d.o
"_glutInitContextFlags", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextProfile", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutInitContextVersion", referenced from:
_InitWindow in chapter-c8ba2d.o
"_glutSetOption", referenced from:
_InitWindow in chapter-c8ba2d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I understand correctly, the problem is tha -framework GLUT means that you're linking against the GLUT framework shipped by Apple with macOS and located inside /System/Library/Frameworks folder. This GLUT implementation unfortunately lacks some functionality provided by current version of FreeGLUT. And OpenGLBook.com tutorials use some of this functionality, thus, as it is mentioned many times here, the code examples have to be compiled with FreeGLUT.
There are, of course, few ways this can be done. The easiest one is by installing freeglut by means of the infamous Homebrew package manager and then setting up the CMake project as described below.
So, first install freeglut
brew install freeglut
Then setup CMake project by adding the following lines into CMakeLists.txt
find_package(FreeGLUT CONFIG REQUIRED)
find_package(glew CONFIG REQUIRED)
link_libraries(FreeGLUT::freeglut GLEW::GLEW)

CBLAS mac OS X Undefined symbols for architecture x86_64 error

I'm trying different C linear algebra libraries for my projects and now I want to learn BLAS (CBLAS). I am trying to follow the tutorial here. I realised that cblas is already built in the xcode and by adding the flag
gcc foo.c -framework Accelerate
or
gcc foo.c -lcblas
I can remove most of the errors I had before. however there is a final error which I can not find anywhere on the internet.
Undefined symbols for architecture x86_64:
"_printVector", referenced from:
_main in blas1C-63e43d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm not sure where I'm making the mistake but there are a number of possibilities:
I need to instal BLAS/CBLAS from the netlib website. I actually tried to do this but I could manage to build the library from the source code! it would be great if somebody could make a Homebrew formula. then if the original CBLAS library installed then I need to learn about the gcc flags for compiling and how to link the libraries.
or there are syntax differences between the original CBLAS from netlib and the one built in the xcode and I need to change the code.
thanks for your help in advance.
P.S. I'm trying to compile the codes blas1C.c, blaio.c, blaio.h from the page I mentioned.
edit 1: oh my! I just realised that I have made a horrible mistake. the printVector function missing is not part of CBLAS but a function made by the author of the blog. the only thing I had to do was just to compile the blaio.c file as well. so the correct gcc command should be
gcc blas1C.c blaio.c -lcblas
or
gcc blac1C.c blaio.c -framework Accelerate
That function comes from the blasio.c in the website and declared in blasio.h
You need to build blasio and link to it

libiconv issue when compiling SDL 2.0 on Mac OS X 10.6

I'm trying to compile SDL 2.0 on OS X 10.6, but I've been getting this message:
Undefined symbols:
"_libiconv_open", referenced from:
_SDL_iconv_string in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
"_libiconv", referenced from:
_SDL_iconv in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
"_libiconv_close", referenced from:
_SDL_iconv_string in SDL_iconv.o
_SDL_iconv_string in SDL_iconv.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I've read the issues over at https://github.com/mxcl/homebrew/issues/894 and How to replace MacPort's libiconv with Mac's default 64-bit version? to see if either of them help. I uninstalled everything that MacPorts had since those two seemed to suggest that the issue was related to multiple versions of the same library (libiconv it had installed had way too many dependencies to manually uninstall them all, and I didn't need them anymore), but am still getting the same error. Any ideas?
So it turns out after uninstalling the MacPorts copy, I still had 2 copies of libiconv on my computer that were different. One was in /usr/lib, and the other was in /usr/local/lib. Compiling with the one in /usr/lib produced the error above, however adding -L/usr/local/lib to the EXTRA_LDFLAGS variable in the Makefile worked.
I found a simple solution. Just add 2 more parameters when configure SDL source:
./configure CPPFLAGS='-I/opt/local/include' LDFLAGS='-L/opt/local/lib'

OpenSSL ecc function with macos xcode?

I am trying to use the function EC_KEY_new_by_curve_name(NID_secp256k1) present in openssl. However, when I compile, I get the following error:
undefined symbols for architecture x86_64:
"_EC_KEY_new_by_curve_name", referenced from:
CKey::CKey() in bitcoin.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have been hinted that macos openssl is not compile with the ECC extensions. Is the problem? If so, how do I correct it?
If you're using the OpenSSL library that comes with MacOS SDK, then it should automatically have 64-bit support built in.
Looking at the first hit on Google, I'm wondering if you are just not including the correct library in your XCode project. Did you get -lcrypto into the project settings, or forget to add libCrypto.dylib to your list of libraries in the project?
On my SnowLeopard (10.6) machine, I see the symbols are defined in libCrypto:
[/usr/lib]:; nm -arch x86_64 libcrypto.0.9.8.dylib | egrep -i new_by_curve
00000000000a4ac0 T _EC_GROUP_new_by_curve_name
00000000000ab540 T _EC_KEY_new_by_curve_name

Problem using C file and Cocoa classes together

I have imported the ffmpeg and SDL libraries into my Xcode project for a Cocoa application.
My project builds and runs successfully with these libraries when my project contains only Cocoa classes, but when I include a C file in my project, the build fails with one warning and 35 errors:
The warning is:
ld: warning: in /Developer/SDKs/MacOSX10.6.sdk/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/libgcc.a, file was built for unsupported file format which is not the architecture being linked (x86_64)
and the errors:
"___gedf2", referenced from:
_eval_expr in libavcodec.a(eval.o)
_quantize_lpc_coefs in libavcodec.a(lpc.o)
_rc_2pass2_before in libxvidcore.a(plugin_2pass2.o)
"___ledf2", referenced from:
_qp2bits in libavcodec.a(ratecontrol.o)
_get_qscale in libavcodec.a(ratecontrol.o)
"___umodsi3", referenced from:
_vorbis_parse_setup_hdr_codebooks in libavcodec.a(vorbis_dec.o)
_vorbis_parse_setup_hdr_codebooks in libavcodec.a(vorbis_dec.o)
For running the code, these are the configuration settings:
Active architecture: x86_64
Architecture in project setting: Standard (32/64_bit universal)
Mac OS X version: 10.6.3
Xcode version: 3.2.3
Also, the same code is building and running successfully with the same settings on my other system, a Mac Mini.
If anyone has any idea what I am missing then please help. Thanks.
Read the error message carefully (emphasis mine):
ld: warning: in /Developer/SDKs/MacOSX10.6.sdk/usr/lib/gcc/powerpc-apple-darwin10/4.2.1/libgcc.a, file was built for unsupported file format which is not the architecture being linked (x86_64)
Looks like you're mixing PowerPC and Intel libraries somehow. You're compiling for x86_64 but you're picking up a libgcc.a for PowerPC.
Those symbols with all the leading underscores are probably supposed to come from libgcc.a but you're not linking the x86_64 version of libgcc.a so they're missing in action and chaos ensues. Sounds like something in your xcode configuration is confused.

Resources