Compile OpenGL program in Mac Terminal - c++

I am using Mac Os X 10.9.5 fully updated with XCode version 6.0.1. I have also installed the command line utilities that have to be installed after installing XCode. I am using GLFW and GLEW in my openGL libraries. GLEW was installed manually while GLFW was installed with Macports.
I want to do coding without XCode as I have been doing it on Windows (with MingW) and Linux. I have googled how to compile the program and also searched on stackoverflow.
So, currently my compile command looks like this
(This is a C++ program)
g++ -framework Cocoa -framework OpenGL -framework CoreFoundation -framework CoreVideo -framework IOKit -L/usr/lib -L/usr/local/lib -I/usr/include/GLFW -I/usr/local/include/GL main.cpp -o main
I keep on getting the following error:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main-ba7a57.o
"_glfwInit", referenced from:
_main in main-ba7a57.o
"_glfwMakeContextCurrent", referenced from:
_main in main-ba7a57.o
"_glfwTerminate", referenced from:
_main in main-ba7a57.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The program is:
#define GLEW_STATIC
#include <glew.h>
#include <glfw3.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
GLFWwindow* window;
if(!glfwInit()) exit(EXIT_FAILURE);
window = glfwCreateWindow(1024, 768, "glfw", NULL, NULL);
if(!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
const GLubyte* version = glGetString(GL_VERSION);
cout << "OpenGL version: " << version << endl;
glfwTerminate();
return 0;
}

It's only a guess, but try adding `-lglfw' to the end of your command-line. This tells g++ to compile against libglfw.

The fact that the error says Undefined symbols for architecture x86_64 makes me think perhaps the versions of the libraries you installed were built to target 64 bit architectures. Remember when choosing libraries to install that you should be choosing 32 bit or 64 bit versions based on which architectures you intend to target, not which architecture your development machine uses.
See this other question for reference.

Related

libuvc program does not compile

I'm trying to use libuvc in one of my C/C++ projects. I succesfully compiled and installed both libusb and libuvc from source and when I attempt to compile the following code using gcc:
#include "libuvc/libuvc.h"
int main (int argc, const char **argv) {
uvc_init(NULL, NULL);
}
I get the following error:
Undefined symbols for architecture x86_64:
"_uvc_init", referenced from:
_main in main-de2855.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 on MacOS High Sierra 10.13.1 x64.
I used the command:
gcc main.c -luvc -o main
and it worked! Adding the -luvc flag solved my problem, but I don't understand why I need to use it. I've never needed flags before when working with other C/C++ dependencies.

Compiling Allegro 5 Program through command line on a Mac

I followed to steps to build and install Allegro 5 from their wiki (found here: https://wiki.allegro.cc/index.php?title=Main_Page) and seemingly succeeded with no problems.
allegro was installed to the following (as the wiki suggests) /usr/local/include and usr/local/lib and I have confirmed allegro is there.
I then wrote the following code in vim:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
I am new to using Unix and have only ever compiled c++ programs with g++ that were simple hello world files with no libraries needed.
Therefore after searching around on the internet I tried the following commands:
g++ hello.cpp -o hello `pkg-config --libs allegro-5`
resulting in the following:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbols not found for architecture x86_64
clang: error: linker command failed with exit code 1
BTW, I used homebrew to install dependencies instead of macports
brew install pkg-config
brew install zlib
etc...
It seems like a linking problem.
What am I doing wrong?
try install allegro with homebrew and use gcc -o main main.c -lallegro -lallegro_main
because the allegro_main is a compatibility library that allows the main function to work on all compilers. Required only from OS X.

ld: symbol(s) not found for architecture

First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2

openGL object not found for architecture x86_64

I am working on a project, using both opencv and openGL, compiling with Cmake on OSX. I get this error message:
_glTexImage2D", referenced from:
matToTexture(cv::Mat) in getimages.cpp.o
ld: symbol(s) not found for architecture x86_64
I guess I have to use openGL 64bit instead of 32bit, but I don't know how to specify this.
When compiling for MacOS X you must add the OpenGL framework. The compiler command line option is
-Framework OpenGL

Linker Error when building Xcode project using external library

I am attempting to build a very simple command line application, in Xcode, that will print out basic information about MXF video files. In order to do this I need to use the libmxf, libbmx, and libbmx libraries available for download here:
http://sourceforge.net/p/bmxlib/home/Home/
My C++ code is incredibly simple at this point:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <vector>
#include <bmx/mxf_reader/MXFFileReader.h>
#include <bmx/mxf_reader/MXFGroupReader.h>
#include <bmx/mxf_reader/MXFSequenceReader.h>
#include <bmx/mxf_reader/MXFFrameMetadata.h>
#include <bmx/MXFUtils.h>
#include <bmx/Utils.h>
using namespace std;
using namespace bmx;
#define MXF_OPEN_READ(fn, pf) mxf_disk_file_open_read(fn, pf)
int main(int argc, const char * argv[])
{
std::vector<const char *> filenames;
std::cout << "mxfheader: execution beginning...\n";
for (int cmdln_index = 0; cmdln_index < argc; cmdln_index++) {
if (!check_file_exists(argv[cmdln_index])) {
if (argv[cmdln_index][0] == '-') {
fprintf(stderr, "Unknown argument '%s'\n", argv[cmdln_index]);
} else {
fprintf(stderr, "Failed to open input filename '%s'\n", argv[cmdln_index]);
}
return 1;
}
filenames.push_back(argv[cmdln_index]);
}
std::cout << filenames[0] << "\n";
return 0;
}
When I compiled the BMX library, I was sure to run configure with 64-bit support, like so:
./configure --build=x86_64-apple-darwin11.4.2 --host=x86_64-apple-darwin11.4.2 CFLAGS="-arch x86_64" CXXFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" CC=clang CXX=clang++
In the XCode Project under Build Settings, I have added /usr/local/lib to my Search Paths. Under Build Phases, I have added "libbmx-0.1.3.dylib", "libMXF-1.0.4.dylib", and "libMXF++-1.0.4.dylib" to the "Link Binary With Libraries" section.
I have verified that these libraries are, indeed, 64-bit ( file libbmx-0.1.3.dylib returns libbmx-0.1.3.dylib: Mach-O 64-bit dynamically linked shared library x86_64 ).
Every time I try and build the application, I get the following linker error:
Ld /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader normal x86_64
cd /Users/ned/Documents/src/mxfheader
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -L/usr/local/lib -F/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -filelist /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Intermediates/mxfheader.build/Debug/mxfheader.build/Objects-normal/x86_64/mxfheader.LinkFileList -mmacosx-version-min=10.7 -stdlib=libc++ -lbmx-0.1.3 -lMXF-1.0.4 -lMXF++-1.0.4 -o /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader
Undefined symbols for architecture x86_64:
"bmx::check_file_exists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help would be appreciated. Thanks!
Your problem is the option: -stdlib=libc++ in the command line. It's causing a link to the wrong libc++, you need to make it -stdlib=libstdc++, as this is the stdlib that the libbmx library is compiled against.
under the Apple LLVM compiler options for the C++ standard library, select: libstdc++, or pick compiler default (which should choose libstdc++ also)