I have a problem when using OpenGL Loader Generator, which when I try to compile my code, it doesn't work. It throws an error saying undefined reference to any opengl function I use, such as gl::BindBuffer, gl::GenBuffers, etc. I'm using pointer_cpp/func_cpp style.
My simple code that I'm using is
#include "gl_core_3_3.hpp"
#include <GL/glfw.h>
int main(int argc, char *argv[]) {
glfwInit();
glfwOpenWindow(1024, 768, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
gl::exts::LoadTest didLoad = gl::sys::LoadFunctions();
if(!didLoad) {
glfwTerminate();
return 1;
}
return 0;
}
When I compile that, it says undefined reference to gl::sys::LoadFunctions too. The command I'm using to compile is
g++ main.cpp -lglfw -lGL -lGLU
I'm on Arch Linux and using Vim with clang as my IDE.
g++ main.cpp -lglfw -lGL -lGLU
I don't see where you're including the generated source file. It's not a header-only loading system. It doesn't generate a library, but it does generate source code, which must be compiled.
Related
I have a problem while running an executable file with dlopen function used to open shared and sanitized library with a one simple function.
I use precompiled Clang 3.9.0 for Ubuntu 14.04.
My question is: Is it possible to run it properly, so I can look for undefined behavior errors in the library while running an executable ? If the answers is yes, then how ?
I have two files:
//simpledll.cpp
#include <cstdio>
int hehe(int argc) {
int k = 0x7fffffff;
k += argc;
return 0;
}
//dlopen.cpp
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
void* handle;
handle = dlopen("simpledll.so", RTLD_LAZY);
if(!handle) {
fprintf(stderr, "%s\n", dlerror());
}
int (*function)(int) = reinterpret_cast<int (*)(int)> (dlsym(handle, "_Z4hehei"));
if (function == nullptr)
fprintf(stderr, "Nope\n");
else
function(1000); // this yields signed integer overflow
return 0;
}
I have tried to get it to work in two steps (both have failed)
Step I
Compile the executable with:
clang++ dlopen.cpp -ldl --std=c++11 -o dlopen
Compile the library with:
clang++ -fsanitize=undefined -shared -o simpledll.so -fPIC simpledll.cpp
Result:
./dlopen: symbol lookup error: simpledll.so: undefined symbol: __ubsan_handle_add_overflow
Step II (idea from this forum)
Compile the executable as in Step I,
Compile the library with:
clang++ -fsanitize=undefined -shared -Wl,--whole-archive -L/usr/local/lib/clang/3.9.0/lib/linux/ -lclang_rt.ubsan_standalone_cxx-x86_64 -Wl,--no-whole-archive -lclang_rt.ubsan_standalone-x86_64 -Wl,--no-whole-archive -o simpledll.so -fPIC simpledll.cpp
Result:
==11478==Sanitizer CHECK failed: /home/development/llvm/3.9.0/final/llvm.src/projects/compiler-rt/lib/ubsan/ubsan_init.cc:61 ((UBSAN_MODE_UNKNOWN)) != ((ubsan_mode)) (0, 0)
Note that in Step II, if we substitute the function in the shared library with the one that has no undefined behavior code, the program runs without a CHECK failed error. This indicates that UBSAN has found an undefined behavior code, however it was unable to report it properly.
Regards,
Jaszczur
I tried to compile a simple C++ program that uses SDL 2 with the mingw-w64-g++ compiler on my Arch Linux (64bits).
For this I downloaded SDL2-devel-2.0.4-mingw.tar.gz from here
prog.cpp:
#include <SDL.h>
int main ()
{
SDL_Init (SDL_INIT_VIDEO);
SDL_Window *sdlWnd = SDL_CreateWindow ("Test", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent (&event)) {
if (event.type == SDL_QUIT) {
running = false;
break;
}
}
}
return 0;
}
Makefile:
GPP = x86_64-w64-mingw32-g++
prog.exe: prog.o
$(GPP) -o prog.exe prog.o -LSDL2-2.0.4/lib/x64 -lSDL2main -lSDL2
prog.o: prog.cpp
$(GPP) -o prog.o -c -ISDL2-2.0.4/include prog.cpp
Now making gives the error:
x86_64-w64-mingw32-g++ -o prog.exe prog.o -LSDL2-2.0.4/lib/x64 -lSDL2main -lSDL2
Warning: corrupt .drectve at end of def file
SDL2-2.0.4/lib/x64/SDL2main.lib(./x64/Release/SDL_windows_main.obj):(.text[main]+0x1c): undefined reference to `SDL_main'
Why undefined reference to `SDL_main' ? Although I specified -lSDL2main ?
What did I do wrong? :(
Okay, it was because of the main functions signature, that has to be declared as:
int main(int argc, char *argv[])
according to the official SDL FAQ:
Make sure that you are declaring main() as:
#include "SDL.h"
int main(int argc, char *argv[])
You should be using main() instead of WinMain() even though you are
creating a Windows application, because SDL provides a version of
WinMain() which performs some SDL initialization before calling your
main code. If for some reason you need to use WinMain(), take a look
at the SDL source code in src/main/win32/SDL_main.c to see what kind
of initialization you need to do in your WinMain() function so that
SDL works properly.
I'm trying to compile a very simple program in openGL:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace glm;
int main()
{
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Use OpenGL 3.X
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Use openGL X.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create OpenGL context
GLFWwindow* window; // May be needed to make it global
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if(window == NULL)
{
fprintf(stderr, "Failed to pen GLFW window. If you have Intel GPU they are not 3.3 compatible. Try the 2.1 version of the tutorial.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if(glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW.\n");
return -1;
}
}
However when i try to compile it with g++ i'm getting the following errors:
In function `main':
playground.cpp:(.text+0x9): undefined reference to `glfwInit'
playground.cpp:(.text+0x49): undefined reference to `glfwWindowHint'
playground.cpp:(.text+0x58): undefined reference to `glfwWindowHint'
playground.cpp:(.text+0x67): undefined reference to `glfwWindowHint'
playground.cpp:(.text+0x76): undefined reference to `glfwWindowHint'
playground.cpp:(.text+0x85): undefined reference to `glfwWindowHint'
playground.cpp:(.text+0xa4): undefined reference to `glfwCreateWindow'
playground.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
playground.cpp:(.text+0xe5): undefined reference to `glfwMakeContextCurrent'
playground.cpp:(.text+0xeb): undefined reference to `glewExperimental'
playground.cpp:(.text+0xf1): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
I've found some posts to this specific problem and the solution seems to be to pass the required libraries as arguments to g++. However, when i do that, the problem still persists.
This is how i compile the code:
g++ -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -o openGLWindow openGLWindow.cpp
Any help would be appreciated...
when i try to compile it with g++ i'm getting the following errors:
No, your compilation is just fine. You are getting link errors, not compile errors.
The reason for your link errors is that your link command line is completely backwards. Try this instead:
g++ -pthread -o openGLWindow openGLWindow.cpp -lglfw3 -lGLEW -lGLU -lGL -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lrt -ldl
I am not sure the order of libraries above is correct, but it's certainly better than what you have. The order of sources, object files, and libraries on command line matters.
I use this command to build and run my application in a terminal. (move in the application directory with cd exemple/c)
gcc -o main main.c -I/usr/include -lglfw -lGL -GL -glut; sleep 0.1;./main
I see where my applications was by this command in a shell/prompt/terminal
pkg-config --libs --cflags --debug glfw3
How can I use either GCC or the Sublime IDE to compile my SDL based project on Linux?
gcc main.cpp -o main -lSDL2
I have tried this and I just get errors that seem to be pointing to the idea that either the program isn't pointing at the library the right way or the compiler isn't recognizing the library. On Sublime I have essentially done the same thing by creating my own build system for SDL, but it doesn't really seem to do anything at all. What might I be doing wrong?
{
"cmd" : [ "gcc", "$file", "-o", "-lSDL2" ]
{
I hope this question is appropriate on this stack, it can be considered Linux tooling but the Linux and Unix stack doesn't suggest programming questions unless they consist of shell scripting.
Here is just the basic Hello World that I am testing (Obviously not quite formatted correctly, but you get the idea.) :
#include <SDL2/SDL.h>
int main(int argc, char *argv[]) {
int win = 1;
SDL_Event event;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Window", NULL);
SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
while (win) {
SDL_WaitEvent(&event);
if (event.type == SDL_QUIT)
win = 0;
}
SDL_Quit();
return 0;
}
When the program is compiled I use the GCC command posted at the top and get these errors:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:19:37: error: ‘SDL_WM_SetCaption’ was not declared in this scope
SDL_WM_SetCaption("Window", NULL);
^
main.cpp:20:36: error: ‘SDL_HWSURFACE’ was not declared in this scope
SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
^
main.cpp:20:49: error: ‘SDL_SetVideoMode’ was not declared in this scope
SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
^
I have also tried pointing directly to header files in GCC with:
gcc main.cpp -o main -l/usr/include/SDL2
I did check to make sure that the header files were in usr/include/SDL2, maybe the actual library isn't installed in the right place for development?
The errors from GCC were cause by calling functions in SDL2 that didn't exist. Changing the previous SDL Video calls to:
SDL_CreateWindow(
"Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
was the right way to create a window in SDL 2;
Following with the GCC commands:
gcc main.cpp -o main -lSDL2
Compiled the code correctly and thus made it executable. Thank you for pointing that out #HolyBlackCat
I have a code like this below in /root_project/main.cpp:
#include "theoraplayer/TheoraVideoClip.h"
unsigned int tex_id;
TheoraVideoManager* mgr;
TheoraVideoClip* clip;
std::string window_name="glut_player";
bool started=1;
int window_w=800,window_h=600;
void draw()
{
glBindTexture(GL_TEXTURE_2D,tex_id);
TheoraVideoFrame* f=clip->getNextFrame(); //this gives an error!!!
if (f)
{
and the TheoraVideoClip.h file is in /root_project/include/theoraplayer/.
Inside of TheoraVideoClip.h there is this:
TheoraVideoFrame* getNextFrame();
And when I try to compile using g++ -o app main.cpp -lGL -lglut -lGLU
I'm gettin this error:
main.cpp:(.text+0xac2): undefined reference to
`TheoraVideoClip::getNextFrame()'
Anyone knows Why?
Ubuntu 11.10
You also need to link to libtheoraplayer.