Can't compile easy source in C++ and OpenGL (GLFW) in Linux in NetBeans
First things first:
this is my source (I didn't use this characters: <, > in header files.):
That is wrong, and you should. Your current include statements are wrong, and I am actually surprised how it passed the compilation process this way.
You are seeing linker errors in here:
/home/jan/NetBeansProjects/a/main.cpp:12: undefined reference to `glfwInit'
/home/jan/NetBeansProjects/a/main.cpp:16: undefined reference to `glfwCreateWindow'
/home/jan/NetBeansProjects/a/main.cpp:19: undefined reference to `glfwTerminate'
/home/jan/NetBeansProjects/a/main.cpp:22: undefined reference to `glfwMakeContextCurrent'
/home/jan/NetBeansProjects/a/main.cpp:25: undefined reference to `glewExperimental'
/home/jan/NetBeansProjects/a/main.cpp:26: undefined reference to `glewInit'
There might be the following options for the failure:
You are not linking against the library (most likely)
You are not having the library installed (unlikely, based on your description)
You are using symbols not present in the library (again, unlikely)
The most probably reason is that you are not linking against the library, eventually. You should have this set up for the linker:
-lglfw3
Note that you will also need to add everything in the chain that comes up as a dependency when you start adding these, so based on your comment this is the whole chain to add:
-L/usr/local/lib -lglfw3 -pthread -lGLEW -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
Since you are using the Netbeans IDE, you will need to go to the project settings to set it up unless you edit the files in the background manually. Here, you can see a screenshot which demonstrates that you have a linker tab where you can set up all this properly.
I cant get GLEW to work on netbeans in ubuntu 20.04 (C++)
There is a standard way on Linux to get a correct set of libraries to link with your program - please look at man pkg-config
. In your case you need to link everything, which is needed by both GLFW3 and GLEW - so you should call the pkg-config
two times:
hekto@ubuntu:~$ pkg-config --libs glfw3
-lglfw
hekto@ubuntu:~$ pkg-config --libs glew
-lGLEW -lGLU -lGL
Therefore, you need to use -lglfw
instead of -lglfw3
and add two more libraries - -lGLEW
and -lGLU
. These calls to the pkg-config
tool can be directly added to your Makefile
as well.
Can't link GLFW libs
There were two reasons:
- Linkage error because of presence of deprecated glfw2 libs (just do
sudo apt-get purge libglfw-dev
andsudo apt-get purge libglfw2
and reinstall libglfw3. - Freeze. Yup, this is a well-known unity bug. You can for example manually retrieve a valid window pointer to use it with glfw. And you also can use for example gnome or mate.
MinGW Linking GLFW gets errors
I tried your program with g++ and added the flag -lgdi32 with g++.
I was able to get this working successfully with this option.
typically, if you're compiling with g++, you usually need to have -lglfw3 , -lopengl32 , and lgdi32 . it largely depends, also, on where you've put your libs and headers for glfw.
as long as they're in a folder that gcc/g++ can find, there's typically not any problems compiling and building. without the flags, you'll continue to get the undefined errors.
Compile V8 Hello World in Netbeans
-I. -Iinclude Include Paths:
C++ Compiler -> Include Directories
-lrt -ldl -pthread libraries:
Linker -> Libraries -> Add Standard Libraries -> select "Posix Threads", "Realtime" and "Dynamic Loader" (I'm not too sure of the exact names as I'm not on a Linux box at the moment).
-std=c++0x:
C++ Compiler -> C++ Standard -> C++11
-Wl,--start-group out.gn/x64.release/obj/{libv8_{base,libbase,external_snapshot,libplatform,libsampler},third_party/icu/libicu{uc,i18n},src/inspector/libinspector}.a -Wl,--end-group
I guess that all of this needs to go into
Linker -> Additional Options
How to build & install GLFW 3 and use it in a Linux project
2020 Updated Answer
It is 2020 (7 years later) and I have learned more about Linux during this time. Specifically that it might not be a good idea to run sudo make install
when installing libraries, as these may interfere with the package management system. (In this case apt
as I am using Debian 10.)
If this is not correct, please correct me in the comments.
Alternative proposed solution
This information is taken from the GLFW docs, however I have expanded/streamlined the information which is relevant to Linux users.
- Go to home directory and clone glfw repository from github
cd ~
git clone https://github.com/glfw/glfw.git
cd glfw
- You can at this point create a build directory and follow the instructions here (glfw build instructions), however I chose not to do that. The following command still seems to work in 2020 however it is not explicitly stated by the glfw online instructions.
cmake -G "Unix Makefiles"
You may need to run
sudo apt-get build-dep glfw3
before (?). I ran both this command andsudo apt install xorg-dev
as per the instructions.Finally run
make
Now in your project directory, do the following. (Go to your project which uses the glfw libs)
Create a
CMakeLists.txt
, mine looks like this
CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
PROJECT(project)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_BUILD_TYPE DEBUG)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(/home/<user>/glfw /home/<user>/glfw/src)
FIND_PACKAGE(OpenGL REQUIRED)
SET(SOURCE_FILES main.cpp)
ADD_EXECUTABLE(project ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(project glfw)
TARGET_LINK_LIBRARIES(project OpenGL::GL)
If you don't like CMake then I appologize but in my opinion it is the easiest way to get your project working quickly. I would recommend learning to use it, at least to a basic level. Regretably I do not know of any good CMake tutorial
Then do
cmake .
andmake
, your project should be built and linked against glfw3 shared libThere is some way of creating a dynamic linked lib. I believe I have used the static method here. Please comment / add a section in this answer below if you know more than I do
This should work on other systems, if not let me know and I will help if I am able to
Can't get GLFW to link
The problem is that the glfw libraries are being specified to the linker before the object file that depends on them. ld
searches libraries to resolve dependencies only for the dependencies that it knows about at the point in list of files it's processing. So when ld
is searching libglfw.a
it doesn't know about the glfwInit
dependency in main.o
yet. ld
(by default) doesn't go back an search the library again.
Try:
$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDFLAGS)
Also the libraries should probably be specified in a LDLIBS
(or LIBS
) variable - LDFLAGS
is conventionally use for linker options:
CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include
SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello
all: init $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE):
$(CXX) $(LDFLAGS) $(LIB_DIR) -o $@ $(OBJECTS) $(LDLIBS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $@
init:
@mkdir -p "$(OBJ_DIR)"
clean:
@rm -rf $(OBJ_DIR) $(EXECUTABLE)
Related Topics
Addition of Two Pointers in C or C++ Not Supported. Why
Conversion from Int** to Const Int**
When to Use C++ Private Inheritance Over Composition
How Can Cstring Be Passed to Format String %S
Can't Open Txt Files in C++ Program with Visual Studio 2019
How to Pass Std::Unique_Ptr into a Function
How to Save Two Camera's Data But Not Influence Their Picture-Acquire Speed
Does Reinterpret_Cast Lead to Undefined Behavior
Is Maximum_Wait_Objects Really 64
Std::Async Won't Spawn a New Thread When Return Value Is Not Stored
C++ Convert from Lpctstr to Const Char *