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

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)

Related

Cross Compiling for x64 on ARM (Apple Silicon)

It is definitely possible to target Intel when compiling on an Apple Silicon (ARM64) system, as Xcode does that all the time when building universal bundles of an app. However, I am unable to replicate this compiling a C program with make (specifically Stockfish).
What I've tried
I'm invoking make like so: make build ARCH=x86-64-modern COMP=clang (the same command works when I substitute x86-64-modern for apple-silicon). I've tried using the gcc compiler, which also worked when targeting the apple-silicon arch.
The problem
The make build command terminates with a bunch of errors, most importantly:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Above the error is a bunch of lines complaining that a file was built for an "unknown bitcode architecture:
ld: warning: ignoring file <name>.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): <name>.o
So, it seems like the compilation phase succeeds, but the linking phase fails due to missing symbols. How would I acquire and provide the missing symbols to ld such that it can link successfully?

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.
You are missing something like
LDFLAGS+= -ljson-c
to link with the library. It was in the setup instructions for CMake in their README.

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

Resources