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
Related
This is my class code:
#include <SDL2/SDL.h>
#include "graphics.h"
/* Graphics class
* Holds all information dealing with graphics for the game
*/
Graphics::Graphics() {
SDL_CreateWindowAndRenderer(640, 480, 0, &this->_window, &this->_renderer);
SDL_SetWindowTitle(this->_window, "Cavestory");
}
Graphics::~Graphics() {
SDL_DestroyWindow(this->_window);
}
this is the error:
g++ "-LC:\\MinGW\\lib" -o cavestory-developement.exe "source\\src\\cavestory-developement.o" "source\\src\\graphics.o" -lmingw32 -lSDL2main -lSDL2
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: source\src\graphics.o: in function `ZN8GraphicsC2Ev':
C:\Users\John Park\eclipse-workspace\cavestory-developement\Debug/../source/src/graphics.cpp:10: undefined reference to `SDL_CreateWindowAndRenderer'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\John Park\eclipse-workspace\cavestory-developement\Debug/../source/src/graphics.cpp:11: undefined reference to `SDL_SetWindowTitle'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: source\src\graphics.o: in function `ZN8GraphicsD2Ev':
C:\Users\John Park\eclipse-workspace\cavestory-developement\Debug/../source/src/graphics.cpp:17: undefined reference to `SDL_DestroyWindow'
collect2.exe: error: ld returned 1 exit status
19:06:06 Build Failed. 5 errors, 0 warnings. (took 223ms)
______________________________________________________________________________________________________
not sure why SDL_SetWindowTitle is recognized (shows parameters and details about the function) when I hover over it with my mouse, but SDL_CreateWindowAndRenderer and SDL_DestroyWindow are not.
It compile fine for me on Fedora 32 x86_64, both as a native program and as a Windows program:
echo 'struct Graphics { Graphics(); ~Graphics(); SDL_Window *_window; SDL_Renderer *_renderer; } g;int main(int argc,char **argv){return 0;}' >graphics.h;g++ -o sdl2 sdl2.C -Wall -g $(pkg-config --cflags --libs sdl2);sudo dnf install mingw64-SDL2 mingw64-gcc-c++;x86_64-w64-mingw32-c++ -o sdl2.exe sdl2.C -Wall -g $(/usr/x86_64-w64-mingw32/sys-root/mingw/bin/sdl2-config --cflags --libs);(d=$PWD;cd /usr/x86_64-w64-mingw32/sys-root/mingw/bin;wine64 $d/sdl2.exe)
I'm trying to compile a DLIB example: http://dlib.net/image_ex.cpp.html
and I copied it word-for-word:
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// make sure the user entered an argument to this program
if (argc != 2)
{
cout << "error, you have to enter a BMP file as an argument to this program" << endl;
return 1;
}
And I used this command:
g++ dlib1.cpp -o dlib1 -std=c++11 -O3 -I ~/Packages/dlib/ -lpthread -lX11 -ljpeg -lpng -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT
And despite that command working fine months ago: [Can't include the JPEG_SUPPORT headers in a dlib cpp file ] this time, it gave me hundreds of errors:
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x7e): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x94): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xaa): undefined reference to `dlib::png_loader::is_graya() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xd0): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x176): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x1ce): undefined reference to `dlib::png_loader::is_graya() const'
What exactly is going wrong here?
Some of the samples need a command line such as ...
g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
I guess source.cpp is a "library as cpp code" link : dlib compile
I am trying to compile SFML simple code:
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
int main(int argc, char* argv[])
{
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("127.0.0.1", 6000);
if (status != sf::Socket::Done)
{
// error...
}
return 0;
}
All libs and dependencies are installed:
sudo apt-get install libsfml-dev
sudo apt-get build-dep libsfml
I am using two methods:
g++ -c main.cpp
g++ -o main main.o -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
g++ -c main.cpp -I/home/x64joxer/SFML-2.4.0/include
g++ -o main main.o -L/home/x64joxer/SFML-2.4.0/lib -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
But I still have the same problem:
main.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `sf::TcpSocket::TcpSocket()'
main.cpp:(.text+0x38): undefined reference to `sf::IpAddress::IpAddress(char const*)'
main.cpp:(.text+0x54): undefined reference to `sf::TcpSocket::connect(sf::IpAddress const&, unsigned short, sf::Time)'
main.o: In function `sf::TcpSocket::~TcpSocket()':
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x30): undefined reference to `sf::Socket::~Socket()'
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x56): undefined reference to `sf::Socket::~Socket()'
main.o:(.rodata._ZTIN2sf9TcpSocketE[_ZTIN2sf9TcpSocketE]+0x10): undefined reference to `typeinfo for sf::Socket'
collect2: error: ld returned 1 exit status
I read many tutorials and topics at the forum but I still do not know how too fix it.
My system is Kubntu 15.
Can anyone know how to fix it?
You are linking with graphics, system and window but not with network. Did you try adding -lsfml-network ?
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.
I'm trying to compile the following sample code available at XERCES site:
#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
XERCES_CPP_NAMESPACE_USE
int main(int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
with,
g++ -g -Wall -pedantic -L/usr/lib -lxerces-c -o xercesTest xercesTest.cpp
giving me the following linking error:
/tmp/ccYIHCfR.o: In function `main':
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLUni::fgXercescDefaultLocale'
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_2_8::PanicHandler*, xercesc_2_8::MemoryManager*, bool)'
/home/cjmv/temp/xercesTest.cpp:18: undefined reference to `xercesc_2_8::XMLPlatformUtils::Terminate()'
/tmp/ccYIHCfR.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_2_8::XMLException'
collect2: ld returned 1 exit status
I've installed xerces-c28 and xerces-c2-dev through aptitude on my ubuntu-server 12.04
Any help would be appreciated.
Put the library last on the command line:
g++ -g -Wall -pedantic -L/usr/lib -o xercesTest xercesTest.cpp -lxerces-c
include the lib path of xerces:
try this
g++ -I/<xerces-c 2.8.0 path>/include -c xercesTest.cpp
g++ -L/<xerces-c 2.8.0 path>/lib -lxerces-c xercesTest.o