i stripped all classes from project, which left three errors like this:
error_code.hpp:222: undefined reference to `boost::system::generic_category()
using Netbeans IDE
clicked on file -> Project Properties -> Linker
next to Libraries, added libboost_system.so
which got rid of the three undefined reference to boost::system::generic_category()
now it gives:
CLEAN SUCCESSFUL (total time: 123ms)
cd '/home/michaeleric/NetBeansProjects/NNW'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/michaeleric/NetBeansProjects/NNW'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/nnw
make[2]: Entering directory `/home/michaeleric/NetBeansProjects/NNW'
mkdir -p build/Debug/GNU-Linux/_ext/d028d44d
rm -f "build/Debug/GNU-Linux/_ext/d028d44d/CGrid.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/GNU-Linux/_ext/d028d44d/CGrid.o.d" -o build/Debug/GNU-Linux/_ext/d028d44d/CGrid.o ../../Desktop/NN_22_05_17/CGrid.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -o dist/Debug/GNU-Linux/nnw build/Debug/GNU-Linux/_ext/d028d44d/CGrid.o -L/usr/include/boost -lboost_system
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux/nnw] Error 1
make[2]: Leaving directory `/home/michaeleric/NetBeansProjects/NNW'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/michaeleric/NetBeansProjects/NNW'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 6s)
what does this error mean and how to fix?
class that rest requires
#include </usr/include/boost/thread/thread.hpp>
#include </usr/include/boost/thread/xtime.hpp>
#include </usr/include/boost/iostreams/write.hpp>
#define byte unsigned char
#define Thread boost::thread
class CGrid
{
Thread* GetThread();
bool SetThread(Thread* thread);
};
If I compile this with
g++ test.cpp -lboost_system
I get the same linker error which is
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
However, if I add an empty main function, it compiles fine. (I also replaced your ugly #define replacements with typedef.)
#include </usr/include/boost/thread/thread.hpp>
#include </usr/include/boost/thread/xtime.hpp>
#include </usr/include/boost/iostreams/write.hpp>
typedef unsigned char byte;
typedef boost::thread Thread;
class CGrid
{
Thread* GetThread();
bool SetThread(Thread* thread);
};
int main() {}
If this is meant to be an object file and main is in another translation unit, compile it as such with the -c parameter:
g++ -c test.cpp -lboost_system
Related
I'm trying to use gnuplot_i, a gnuplot interface for ANSI-C, for my C++ project. (See here in the download section)
It seems to just consist of one header and one source file. The Makefile just creates an object file, so I decided to completely integrate those two files in my project. However I'm getting errors of undefined references to functions that are implemented in aforementioned source file.
Consider the following simplified example (main.cpp):
#include <gnuplot_i.h>
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
The header file can be obtained here and the source file here.
The error I get is the following:
build/Debug/GNU-Linux/main.o: In function `main':
/<some path>/proj/test/main.cpp:18: undefined reference to `gnuplot_init()'
collect2: error: ld returned 1 exit status
However gnuplot_init() is implemented in the source file which gets compiled to an object file which is then used to link the program. You can see this in the full log below. Also the generated object file contains the necessary symbol:
$ nm gnuplot_i.o | grep gnuplot_init
0000000000000000 T gnuplot_init
full log:
cd '/<some path>/proj/test'
/usr/bin/make -f Makefile CONF=Debug clean
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory '/<some path>/proj/test'
rm -f -r build/Debug
rm -f dist/Debug/GNU-Linux/test
make[1]: Leaving directory '/<some path>/proj/test'
CLEAN SUCCESSFUL (total time: 52ms)
cd '/<some path>/proj/test'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/<some path>/proj/test'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/test
make[2]: Entering directory '/<some path>/proj/test'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/gnuplot_i.o.d"
gcc -c -g -I. -MMD -MP -MF "build/Debug/GNU-Linux/gnuplot_i.o.d" -o build/Debug/GNU-Linux/gnuplot_i.o gnuplot_i.c
gnuplot_i.c: In function ‘gnuplot_tmpfile’:
gnuplot_i.c:696:5: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(unx_fd);
^~~~~
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++ -c -g -I. -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -o dist/Debug/GNU-Linux/test build/Debug/GNU-Linux/gnuplot_i.o build/Debug/GNU-Linux/main.o
build/Debug/GNU-Linux/main.o: In function `main':
/<some path>/proj/test/main.cpp:18: undefined reference to `gnuplot_init()'
collect2: error: ld returned 1 exit status
make[2]: *** [nbproject/Makefile-Debug.mk:64: dist/Debug/GNU-Linux/test] Error 1
make[2]: Leaving directory '/<some path>/proj/test'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/<some path>/proj/test'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2
BUILD FAILED (exit value 2, total time: 304ms)
As I'm using an auto-generated buildsystem from NetBeans the Makefiles are quite big and complex. But it should be pretty obvious from the log which commands have been issued. What is exactly wrong here? Is it a problem that I link C-Object files and C++-Object files? From my understanding it shouldn't.
Well the answer came to me the moment I pressed send.
I'm obviously missing an extern "C"
extern "C" {
#include <gnuplot_i.h>
}
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
I'm new to C++. I'm trying to run a simple example openGL application.
I have downloaded the libraries that are used from the following locations.
http://www.glfw.org/index.html
http://glew.sourceforge.net/
http://glm.g-truc.net/0.9.6/index.html
I am using NetBeans IDE as my C++ IDE. I think I have specified the correct Include Directories because the program compiles without any errors. Following is my include path.
../../../../../gl/glm;../../../../../gl/glew-1.12.0/include;../../../../../gl/glfw-3.1.bin.WIN64/include/GLFW
The command that runs when I compile is the following
CLEAN SUCCESSFUL (total time: 552ms)
C:\MinGW\msys\1.0\bin\make.exe -f nbproject/Makefile-Debug.mk build/Debug/MinGW-Windows/main.o
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++ -c -g -I../../../../../gl/glm -I../../../../../gl/glew-1.12.0/include -I../../../../../gl/glfw-3.1.bin.WIN64/include/GLFW -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
COMPILE FILE SUCCESSFUL (total time: 4s)
But when running the application I get the following errors.
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_1.exe
make.exe[2]: Entering directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_1 build/Debug/MinGW-Windows/main.o -L../../../../../gl/glfw-3.1.bin.WIN64/lib-mingw -L../../../../../gl/glew-1.12.0/bin/Release/x64
build/Debug/MinGW-Windows/main.o: In function `main':
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:19: undefined reference to `glfwInit'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:25: undefined reference to `glfwWindowHint'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:26: undefined reference to `glfwWindowHint'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:27: undefined reference to `glfwWindowHint'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:28: undefined reference to `glfwWindowHint'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:31: undefined reference to `glfwCreateWindow'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:34: undefined reference to `glfwTerminate'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:37: undefined reference to `glfwMakeContextCurrent'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:40: undefined reference to `_imp__glewInit#0'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:46: undefined reference to `glfwSetInputMode'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:49: undefined reference to `glClearColor#16'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:55: undefined reference to `glfwSwapBuffers'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:56: undefined reference to `glfwPollEvents'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:59: undefined reference to `glfwGetKey'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:60: undefined reference to `glfwWindowShouldClose'
C:\Users\name\Documents\NetBeansProjects\CppApplication_1/main.cpp:63: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/cppapplication_1.exe] Error 1
make.exe[2]: Leaving directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 5s)
Under the Build/Linker section, I have given the following as "Additional Library Directories"
../../../../../gl/glew-1.12.0/lib/Release/x64;../../../../../gl/glfw-3.1.bin.WIN64/lib-mingw
I am using MinGW. What am I doing wrong here?
Update:
I supplied the libaries using the -l option. Now I get another error like the following.
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make.exe[1]: Entering directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
rm -f -r build/Debug
rm -f dist/Debug/MinGW-Windows/cppapplication_1.exe
make.exe[1]: Leaving directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
CLEAN SUCCESSFUL (total time: 1s)
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_1.exe
make.exe[2]: Entering directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++ -c -g -I../../../../../gl/glm -I../../../../../gl/glew-1.12.0/include -I../../../../../gl/glfw-3.1.bin.WIN64/include/GLFW -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/cppapplication_1 build/Debug/MinGW-Windows/main.o -L../../../../../gl/glew-1.12.0/bin/Release/x64 -L../../../../../gl/glfw-3.1.bin.WIN64/lib-mingw -lglfw3 -lglew32
../../../../../gl/glew-1.12.0/bin/Release/x64/glew32.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
make.exe[2]: *** [dist/Debug/MinGW-Windows/cppapplication_1.exe] Error 1
make.exe[2]: Leaving directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/name/Documents/NetBeansProjects/CppApplication_1'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/kbhit
gmake[2]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -Iusr/include/ -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/kbhit build/Debug/GNU-Linux-x86/main.o -L/usr/include/ncurses -L/usr/include/ncurses6 -L/usr/include/ncursesw -lncurses.h
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -lncurses.h
collect2: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/kbhit] Error 1
gmake[2]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 477ms)
It appears it cannot find ''lcurses'' but I never defined for it to look for lcurses. I actually added ncurse libraries to be linked.
I have navigated to /usr/include/ncurses and there is in fact the files there should be.
I don't fully know what is going on through this whole compiling stuff it is a little beyond me. I just know that I was told to link the libraries I need. ncurses.h is there this is how I have my code.
#include <iostream>
#include <ncurses/ncurses.h>
using namespace std;
bool kbhit()
{
int ch = getch();
if(ch != ERR)
{
ungetch(ch);
return true;
}
return false;
}
int main() {
while(!kbhit())
{
cout << "no input";
}
cout << "Mummy, it's over.";
return 0;
}
Any help would be pleasantly welcome; I am lost.
this is not a problem with your c++ code, it's more a problem with your build enviroment, are you using netbeans IDE?
This code as far as I can tell does not require ncruses at all, and the link directive that NetBeans? has set up for you points to a ncruses.h file where it should be something like this "-Wl,-lncurses" or only "-lncurses". So a guess is that somewhere in netbeans where you should point out the library name you have written ncruses.h instead of only ncruses(i think, not sure)
try to ask pkg-config what parameters for ncurses should be on your machine "pkg-config --libs ncurses" and "pkg-config --cflags ncurses"
Okay so I had originally been attempting to use some headers that were supposedly for windows only, my bad, but I've gone and just reproduced what I need using curses.h. However I am still receiving the exact same kind of error.
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/kbhit
gmake[2]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/kbhit build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function `kbhit()':
/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `stdscr'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `wgetch'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:23: undefined reference to `ungetch'
collect2: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/kbhit] Error 1
gmake[2]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/josh/Projects/Testing grounds/kbhit'
gmake: *** [.build-impl] Error 2
So, I am not 100% sure that the code should even work as I expect it to. I am just trying to compile this to test it. According to the curses.h documentation getch is supposed to return the value ERR if no keys are queued. I don't really know what else is required here, I thought all I needed to do was include the header the definitions were in. It seems like that isn't enough though, there must be something I have missed. Here is the short test I am trying to compile
#include <cstdlib>
#include <iostream>
#include <curses.h>
#include <ncurses.h>
using namespace std;
bool kbhit()
{
int ch = getch();
if(ch != ERR)
{
ungetch(ch);
return true;
}
return false;
}
int main() {
while(!kbhit())
{
cout << "no input";
}
cout << "Mummy, it's over.";
return 0;
}
You're not linking against the curses library. You need to provide -lncurses to the line that links your executable in your makefile.
Example:
g++ -o myprogram myprogram.cpp -lncurses
Also note that the -lx arguments (where x is your library) should always follow the source and object files.
You also need to link against the library, not just compile against the header. Add -lncurses to the link command. (Also see man ncurses.)
I'm trying to compile a very simple program that makes a connection to a database and prints the database name. I'm able to compile on the command line but am unable to do so in Netbeans. I'm using Netbeans IDE 6.9.1 on Ubuntu 10.04 and am using g++ 4.4.3 and libpqxx 2.6.9 installed using apt-get. Code follows:
#include <iostream>
#include <pqxx/connection>
using namespace std;
using namespace pqxx;
int main(int argc, char *argv[])
{
try
{
// Set up a connection to the backend
connection C(argv[1]);
cout << C.dbname() << endl;
}
catch (const exception &e)
{
// All exceptions thrown by libpqxx are derived from std::exception
cerr << "Exception: " << e.what() << endl;
return 2;
}
return 0;
}
I can successfully compile using the following:
g++ -g -o example -L/usr/lib/ -lpqxx -lpq dbtest001.cpp
Netbeans fails with this:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/dev/work/cpptest'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cpptest
make[2]: Entering directory `/home/dev/work/cpptest'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/cpptest build/Debug/GNU-Linux-x86/main.o
make[2]: Leaving directory `/home/dev/work/cpptest'
make[1]: Leaving directory `/home/dev/work/cpptest'
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/dev/work/cpptest/main.cpp:13: undefined reference to `pqxx::connection_base::dbname()'
build/Debug/GNU-Linux-x86/main.o: In function `connect_direct':
/usr/include/pqxx/connection.hxx:84: undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/include/pqxx/connection.hxx:84: undefined reference to `vtable for pqxx::connect_direct'
build/Debug/GNU-Linux-x86/main.o: In function `~connect_direct':
/usr/include/pqxx/connection.hxx:82: undefined reference to `vtable for pqxx::connect_direct'
/usr/include/pqxx/connection.hxx:82: undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
build/Debug/GNU-Linux-x86/main.o: In function `basic_connection':
/usr/include/pqxx/basic_connection.hxx:61: undefined reference to `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)'
/usr/include/pqxx/basic_connection.hxx:62: undefined reference to `pqxx::connection_base::init()'
build/Debug/GNU-Linux-x86/main.o: In function `~basic_connection':
/usr/include/pqxx/basic_connection.hxx:70: undefined reference to `pqxx::connection_base::close()'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cpptest] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 622ms)
Can anyone offer any insight into why this is failing? I've been away from C++ for a while so am not as sharp at working out where things have gone wrong as I used to be.
g++ -o dist/Debug/GNU-Linux-x86/cpptest build/Debug/GNU-Linux-x86/main.o
You'll notice that Netbeans hasn't added the -L and -l options to the linker command like you did. This is causing the 'undefined reference' errors -- it's not linking your program to the pqxx library.
You'll need to edit your Netbeans project to set up the proper library path and dependencies.
I am also new to Netbeans, this picture might help others as well.