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

I am trying to compile a C program for my CS class. I have Command Line tools installed on my Mac, so I probably have OpenGL. The program description was made for Ubuntu and it says for me to compile using:
gcc -Wall -ansi -pedantic -O2 main.o graphic.o imagem.o io.o -o ep2 -lGL -lGLU -lglut
I ran that and it said:
ld: library not found for -lGL
What flags should I use? What do I do?

In MacOS X you're not using libraries to include system level APIs, but Frameworks. The proper command line to compile this program would be
gcc -Wall -ansi -pedantic -O2 \
main.o graphic.o imagem.o io.o \
-o ep2 \
-framework OpenGL -lGLU -lglut
Note that GLU is probably part of the OpenGL framework as well. And it may be required to install GLUT first.

I found you have to use
-framework OpenGL -framework GLUT
Reference

Related

SDL 2 C Compiler Flags

Whenever I run the following, I get undefined references to all the SDL-related functions used in my program:
cc -lSDL2 -lGL *.o
I believe this is caused by the lack of -l linker flags.
GCC arguments are positional, put the link flags after your o files:
gcc *.o -lSDL2 -lGL
Also, if you're on a proper full Linux system I'd recommend using pkg-config to pull compiler/linker flags:
gcc -c main.c `pkg-config sdl2 --cflags`
gcc main.o `pkg-config sdl2 --libs`

SDL2 : Undefined symbols for architecture x86_64 / MAC [duplicate]

I recently moved to linux and i'm having an issue with compiling SDL C programs using gcc.
The command i'm using:
gcc `sdl-config --cflags --libs` -o main main.c
Even by seperating sdl-config flags:
gcc `sdl-config --cflags` -c main.c
gcc `sdl-config --libs` -o main main.o
I'm getting the same error:
/tmp/ccHYyjKd.o: In function `main':
main.c:(.text+0xe): undefined reference to `SDL_SetMainReady'
main.c:(.text+0x18): undefined reference to `SDL_Init'
main.c:(.text+0x31): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x54): undefined reference to `SDL_MapRGB'
main.c:(.text+0x6b): undefined reference to `SDL_FillRect'
main.c:(.text+0x77): undefined reference to `SDL_Flip'
main.c:(.text+0x83): undefined reference to `SDL_WaitEvent'
main.c:(.text+0x90): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
My very simple program:
#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED
#include <SDL/SDL.h>
int main()
{
// SDL Initialize
SDL_SetMainReady();
SDL_Init(SDL_INIT_VIDEO);
// Screen Initialize
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
Uint32 screenColor = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, NULL, screenColor);
SDL_Flip(screen);
// Main Program Loop
SDL_Event event;
do
{
SDL_WaitEvent(&event);
} while (event.type != SDL_QUIT);
// SDL Quit
SDL_Quit();
return 0;
}
Order of arguments to gcc matters a lot.
Read about Invoking GCC (and documentation of binutils, which gcc uses). Then replace
gcc `sdl-config --libs` -o main main.o
with
gcc main.o `sdl-config --libs` -o main
Better yet, learn how to use GNU make (it is often using GNU bash) and use a Makefile inspired by this answer...
Also, always pass -Wall -g to gcc until your program is bug-free (then use -Wall -O2)
Take inspiration from open source programs on github or gitlab using SDL. Consider also using other open source libraries and frameworks, such as Qt, SFML, GTKmm, etc... And study their example code.
Add -lSDL with gcc compile command. This will add sdl library. Install sdl developement package before compiling.
EDIT:
gcc -o out main.c -lSDL
or
gcc -I/usr/include/SDL/ main.c -o out -L/usr/lib -lSDL
I See this from /usr/include/SDL2/SDL_main.h
/*
* This is called by the real SDL main function to let the rest of the
* library know that initialization was done properly.
*
* Calling this yourself without knowing what you're doing can cause
* crashes and hard to diagnose problems with your application.
*/
extern DECLSPEC void SDL_SetMainReady(void);
Also check this:
nm /usr/lib/i386-linux-gnu/libSDL.a | grep SDL_SetMainReady
This is not the solution but will allow you to focus on the real problem, I think it is not the compilation process.
Thanks a lot for the advices ! This helped me to solve an old mystery about SDL symbols never found under Linux :-) As wroten in the comments, the order of gcc line is essential. The makefile was not well written and this was causing the breakage you mentionned.
As summary, use : gcc ( flags ) -o name ( include et linking options)
Last but not least, under x86_64, use gdb64, instead of gdb ( was the next headhache ;-)
As example, a little Makefile I wrote myself (ok, not that good, but works)
# Makefile pour le contrôle du robot Arduino
# Controle Robot
# Sauf mention contraire, tout est place sous Licence GPL V2
# Historique :
# Etienne HAMON / création du makefile initial (d'après cours IFT1), Novembre 2014
#
# Corrections du Makefile : Eric Bachard décembre 2014
CC = gcc
C_STANDARD = -std=c99
INCLUDE_DIR = inc -I/usr/include/SDL
SOURCES_DIR = sources
BUILD_DIR = build
APPLICATION_NAME = Controle
FILENAME = ${BUILD_DIR}/${APPLICATION_NAME}
CFLAGS = -Wall -ansi ${C_STANDARD}
LDFLAGS = -lSDL $(sdl-config --static-libs) -lm
DEBUG_SUFFIX = _debug
CFLAGS_DEBUG = -v -gdwarf-2 -DDEBUG
OBJS = ${SOURCES_DIR}/*.c
all : ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}
${FILENAME}: ${OBJS}
${CC} ${CFLAGS} -o $# $^ -I${INCLUDE_DIR} ${LDFLAGS}
${FILENAME}${DEBUG_SUFFIX}: ${OBJS}
${CC} ${CFLAGS} ${CFLAGS_DEBUG} -o $# $^ -I${INCLUDE_DIR} ${LDFLAGS}
clean:
${RM} *.o ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}
${RM} -rf ${BUILD_DIR}/*.dSYM
None of the "popular" answers for this question are correct or working. Some of them have nothing to even do with the question being asked. I have the same problem.
SDL docs give an example for compiling like this: gcc -o main main.c `sdl2-config --cflags --libs`
Yet user is suggesting that it is order of arguments causing the problem!! It is not! In fact, their suggested order is something I have never seen before and is certainly not any kind of standard. SDL answers on this site are very low quality!
Update:
I found a solution for some missing functions. SDL_SetVideoMode does not exist in SDL2. Use SDL_CreateWindow.

gcc cannot find -lglfw3

I'm on a linux system (arch linux specifically) and I'm trying to compile the introduction project from the official glfw page but I cannot seem to get gcc to compile it. If anyone doesn't know what I'm talking about it's this.
Also this is how I'm trying to compile it:
gcc -Iinclude test.c -o test -lglfw3 -lm -lGL -lGLU
and it gives me the following errors:
/usr/bin/ld: cannot find -lglfw3
collect2: error: ld returned 1 exit status
I completely forgot about this question until I got a notification for it. For me the solution was to not use -lglfw3 but rather use -lglfw
If you've installed pkg-config, and glfw3.pc is in the search path, try:
gcc -Iinclude test.c -o test `pkg-config --libs glfw3` -lm -lGL -lGLU
If you only have the static build, use: pkg-config --static --libs glfw3, which will add the dependencies that libglfw3.a requires.
Find libglfw3.a or libglfw3.so on your system
Mention that path to gcc using -L
gcc -Iinclude test.c -o test -L/dir/where/glfw3/resides -lglfw3 -lm -lGL -lGLU

Compiling C file in OS X

I am not familiar with OS X at all, and I need to compile a C file. Here is the code I use in Linux. What is the OS X version of those?
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -c myfile.c -o myfile.o
gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -Wl,-z,relro -o myfile.so myfile.o -L/usr/lib64/R/lib -lR
Thanks!
You need to install Xcode, which is free, and will allow you to install gcc just by typing gcc in Terminal. From there on, you can just compile .c files using it. Also, you might want to just type gcc myfile.c -o myfile instead of adding all of those flags, because the OS X filesystem hiearchy is different from that of Linux, and adding those extra flags might make the command not work.

Cannot compile using ALSA

I am trying to create an C application on Debian GNU/Linux which uses the PortAudio interface. To do this I must compile my program with gcc -lrt -lasound -ljack -lpthread -o YOUR_BINARY main.c libportaudio.a from this docs.
For this I installed libasound2-dev, and I checked where the files are using apt-file search libasound.so, this is the output:
lib32asound2: /usr/lib32/libasound.so.2
lib32asound2: /usr/lib32/libasound.so.2.0.0
lib32asound2-dev: /usr/lib32/libasound.so
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0
libasound2-dev: /usr/lib/x86_64-linux-gnu/libasound.so
So the libasound should be installed correctly, but when I compile my program with this makefile:
DMXTest: main.c libdmx.a
gcc -static -Wall main.c -L. -ldmx -lusb -lrt -lasound -ljack -lfftw3 -g -o main libportaudio.a
I get the following error: /usr/bin/ld: cannot find -lasound.
How can I link this library correctly?
You don't have libasound.a for -static, you will need that, or you can almost certainly just remove -static from the Makefile (likely in LDFLAGS or CFLAGS).
There's is a related Debian bug 522544, and a related Ubuntu bug #993959.
You may be able to build your own libasound from source, though as it also uses other libraries (notably libpthread.so, librt.so and libdl.so) I suspect it may remove some functionality when you build it statically, though it's supported with ./configure --enable-static at build time
(or try --enable-shared=no --enable-static=yes).
FWIW, the use of static binaries is "discouraged" by the glibc maintainers, though I don't agree...
To compile my code i used the following command
gcc -o rec_mic rec_mic.c -lasound
and it works perfectly, without create my own static library.

Resources