Linux Mint Eclipse GLFW 3 Setup problems - c

I have, in my opinion, installed and configured GLFW 3 correctly,
my compiler tells me otherwise.
glfw3.h and glfw3native.h are in urs/local/include/GLFW/.
libglfw3.a is in urs/local/lib/.
In Eclipse, i configured in Project Propreties->C/C++ Build->Settings->GCC C Linker->Libraries the following values : GL, GLU, m, rt, pthread, m, glfw3, X11, Xxf86vm, Xrandr and Xi.
In my project, in Test.c i have the following test code :
/*
============================================================================
Name : Test.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
if (!glfwInit())
exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}
So in my opinion and what i have read one the internet, it seems an alright setup.
When i compile it it gives this :
/usr/local/lib/libglfw3.a(window.c.o): In function `glfwCreateWindow':
window.c:(.text+0x724): undefined reference to `glClear'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `getFBConfigAttrib':
glx_context.c:(.text+0x4d): undefined reference to `glXGetFBConfigAttrib'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `chooseFBConfig':
glx_context.c:(.text+0x7a): undefined reference to `glXGetClientString'
glx_context.c:(.text+0xe5): undefined reference to `glXGetFBConfigs'
//usr/local/lib/libglfw3.a(glx_context.c.o): In function `createLegacyContext':
glx_context.c:(.text+0x41f): undefined reference to `glXCreateNewContext'
Reading this i came to the conclusion that it didn't find OpenGL, but i already made OpenGL projects before and it works, so why can GLFW find it?
Thanks.

After some tickering around and praying for this problem to go away for a couple hours, and days. I have found the solution... and i don't fully understand why linking is so hard.
What i did is just try different Linker Libraries option,
This order is the one that worked for me :
-lglfw3 -lGL -lGLU -lX11 -Xxf86vm -lXrandr -lpthread -lXi -lm
Hope this helps others.

Related

"gsl/gsl_rng.h' file not found" after OSX El Capitan upgrade

I just updated to the newest OSX, El Capitan, and I am having problems with compiling a C program. It compiled fine just before the upgrade of the OS. After it I got a warning message already for my LaTeX text editor, Latexian:
Latexian message
But since I don't use preview or compilation inside the program and compile in the terminal with "latex file.tex" it works fine.
Now my problem is with my .c program which includes one of the GSL libraries, here is my header:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
When compiling I get the following:
performance.c:4:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
I am guessing something changed in the OSX because of these two situations but the latter is a huge problem for me since I'm finishing my thesis! Hope my question is clear, it's my first.
EDIT:
And I'm guessing this is the problem
El Capitan's System Integrity Protection will shift utilities' functions
When compiling with GCC you may have to manually specify the parent directory that contains the gsl subfolder. Similarly you will have to specify the directory to find the libraries in as well. The include directory can be added as a search path to gcc with the -I option, and the library search path with -L. In your case that is done by adding this to your GCC compilation:
-I/usr/local/include -L/usr/local/lib
In my case this line solved this linking error
gcc $(gsl-config --cflags) name_of_file.c $(gsl-config --libs) -o name_of_file
See Wiki
In my case I just needed do install/update gsl
brew install gsl

Undefined reference to WinMain#16 when using SDL

I've been having a lot of trouble getting everything working so that I can start developing on Windows, as apposed to Linux, which is what I normally use when coding. I'm having a rather bizarre issue when trying to compile an SDL program. As soon as I include the SDL library, the program refuses to compile, giving me this error:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a<main.o>: In function 'main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to 'WinMain#16'
collect2: ld returned 1 exist status
I am using MinGW on console.
To give an example, using
gcc -o test main.c
This compiles fine:
#include <stdio.h>
#include <stdlib.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
But as soon as I add #include (even without any SDL functions being called) I get the error mentioned above
Using:
gcc -o test main.c -lSDL
This fails to compile:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
Any help would be greatly appreciated! I read that this was a common issue for people who forget to have a main function, but obviously that's not my issue. I also heard that WinMain is the main function used when dealing with Windows graphical programs, but that's never been an issue for me in the past when I used to develop in Windows more.
I did a little bit of searching for some more information on this error, and I found this page which includes the following informaion:
The only trick in getting this to compile now is to add the include path (eg: -I../SDL/include), the linker path (eg: -L../SDL/lib), and then finally adding the libraries themselves in the right order. Use:
-lmingw32 -lSDLmain -lSDL
Also, don't forget to add the -mwindows flag, if your IDE doesn't add it automatically (in addition to whatever other libraries you want to link). If you don't put them in the right order, you'll get a linker error complaining about the missing symbol WinMain#16.
Try recompiling with those flags above and see whether that makes a difference.

FreeType library and "Undefined reference to FT_Init_FreeType"

Coming from PHP, this is my first experience with C/C++ (so go easy on me). I'm following this tutorial to write a simple script using the FreeType library. The following compiles just fine:
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
FT_Library library;
FT_Face face;
}
This tells me that the FreeType library is readily available to the compiler. However, things break once I try to use any methods. For example, take the following script:
#include <ft2build.h>
#include FT_FREETYPE_H
main() {
int error;
FT_Library library;
error = FT_Init_FreeType(&library);
if (error) {}
FT_Face face;
error = FT_New_Face(library, "/usr/share/fonts/truetype/arial.ttf", 0, &face);
if (error == FT_Err_Unknown_File_Format) {
printf("Font format is unsupported");
} else if (error) {
prinft("Font file is missing or corrupted");
}
}
This script produces the following error upon compiling:
#gcc render.c -I/usr/include/freetype2
/tmp/cc95255i.o: In function `main':
render.c:(.text+0x10): undefined reference to `FT_Init_FreeType'
render.c:(.text+0x30): undefined reference to `FT_New_Face'
collect2: ld returned 1 exit status
Any ideas?
Those are link errors. If they include a Makefile with the demo you are better off using that. Otherwise, you need to add -L and -l options to your compile command line so the compiler (actually the linker, which gets invoked by the compiler behind the scene) knows where to find the FreeType library.
The -L option gives the path to where the code for the library exists. For example
-L/usr/local/lib
And the -l option gives the name of the library. The library named with the -l option is specified in a shortened form, that is you leave off the "lib" in the front and the ".a" in the back. So for example, if the FreeType library was stored in file libfreetype.a , it would show in the -l option as
-lfreetype
e.g.:
gcc render.c -I/usr/include/freetype2 -L/usr/local/lib -lfreetype

Why is curses on linux giving me following error?

Trying to get getch() working to capture key press.
#include <curses.h>
...
...
WINDOW *w;
char f;
w = initscr();
timeout(3000);
f = getch();
endwin();
is giving me following error:-
undefined reference to `wgetch'
undefined reference to `stdscr'
That's a linking error. Are you linking to the curses library correctly?
There are two steps involved in using a library in C.
You #include the relevant header files from your source files. This is so your code knows what signatures of the library functions are. So you're doing this correctly.
When compiling your code, you need to tell the linker to link to the relevant libraries, so it can find the definition of those functions. This is what you're not doing. Assuming you're using gcc then adding -lncurses to the compile line should do it. Here's an explanation of linking.
The above answers are correct but the format is:
gcc -Wall program.c -o name_of_binary -lncurses
When I did:
gcc -Wall -lncurses program.c...
It did not work so apparently it should be tacked on the end.

Undefined reference when using ncurses on linux

I'm trying to start developing a program using ncurses on Linux. I can't even get the Hello World example to compile. Here's the code:
#include <curses.h>
int main()
{
initscr();
printw("Hello, world.");
refresh();
getch();
endwin();
return 0;
}
When I attempt to compile, I get:
hello.c:(.text+0x12): undefined reference to `initscr'
For every one of those called functions.
I installed ncurses via apt-get, and also by downloading the sources and compiling, installing, etc.
I have tried #include both curses.h and ncurses.h.
What is going on?
Have you used the -lcurses option when linking?
Including the header files let the code compile (because the compiler knows what the function call looks like from the .h file), but the linker needs the library file to find the actual code to link into your program.
As Greg Hewgill said, you need to pass in -lcurses or -lncurses to link to the curses library.
gcc -o hello hello.c -lncurses
You also probably mean to use initscr() and getch(). Once I make those substitutions, the above compiles for me.
I was having a similar issue and found a solution which helped me, but was slightly different from the other answers posted here. I was trying to use the panels library with curses and my compile command was:
$ gcc -o hello hello.c -lncurses -lpanel
when I read the other answers, I was baffled because I was including the -lncurses flag, but it still was not compiling, and with similar errors to what you were getting:
$ gcc -o hello hello.c -lncurses -lpanel
/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/libpanel.a(p_new.o): In function `new_panel':
p_new.c:(.text+0x18): undefined reference to `_nc_panelhook'
I finally found my answer in the tldp:
"To use panels library functions, you have to include panel.h and to link the program with panels library the flag -lpanel should be added along with -lncurses in that order."
So, it appears that order is important when using the compile flags! I tried switching the order:
gcc -o hello hello.c -lpanel -lncurses
This allowed it to compile successfully. I know you already have your answer, so I hope this helps someone.
For anyone having similar problems: -lx arguments, where x is your library, should always follow the source and object files.

Resources