I am using g++ compiler. I am getting the errors mentioned below. Kindly let me know what my mistakes are, or any other function to get hold on position of the cursor.
Here's my code:
#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>
#include<iostream>
using namespace std;
int main()
{
char mesg[]="Just a string"; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
printw("Try resizing your window(if possible) and then run this program again");
refresh();
getch();
endwin();
return 0;
}```
/*Here are the errors;
/tmp/ccJV4hnK.o: In function `main':
try9.cpp:(.text+0x34): undefined reference to `initscr'
try9.cpp:(.text+0x3b): undefined reference to `stdscr'
try9.cpp:(.text+0x47): undefined reference to `stdscr'
try9.cpp:(.text+0x60): undefined reference to `stdscr'
try9.cpp:(.text+0x6c): undefined reference to `stdscr'
try9.cpp:(.text+0xbf): undefined reference to `mvprintw'
try9.cpp:(.text+0xe4): undefined reference to `mvprintw'
try9.cpp:(.text+0xf3): undefined reference to `printw'
try9.cpp:(.text+0xf8): undefined reference to `refresh'
try9.cpp:(.text+0xff): undefined reference to `stdscr'
try9.cpp:(.text+0x107): undefined reference to `wgetch'
try9.cpp:(.text+0x10c): undefined reference to `endwin'
collect2: error: ld returned 1 exit status*/
Simply add
#include <curses.h>
I ran into the same problem..
You just have to add -lncurses behind the compilation command..
So for example:
clang test.c -lncurses
gcc test.c -lncurses
An “Undefined Reference” error occurs when we have a reference to object name in our code and the linker is not being able to find the definition when it tries to search for your functions initscr, stdscr, mvprintw, etc in all the linked object files and libraries. You must have missed including the appropriate header files.
#include <curses.h>
Related
I currently have this code:
#include <iostream>
#include <time.h>
int main() {
printf("%i", time(0));
return 0;
}
Compile with gcc:
gcc source.cpp -o program
Error:
/tmp/ccTUJX4u.o: In function `__static_initialization_and_destruction_0(int, int)':
source.cpp:(.text+0x1a9): undefined reference to `std::ios_base::Init::Init()'
source.cpp:(.text+0x1b8): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
I just want to print the current timestamp I tried stuff with the chrono and ctime modules but nothing works is it really that hard to print the current time in C++?
Please help
I'm trying to port over a game that I made using Allegro 5.0 (from Windows to a Raspberry Pi running Raspian). I installed Allegro and moved over all my source files, but when I try to compile using: *g++ -std=c++0x .cpp -o SpaceFighter $(pkg-config --libs allegro-5) I get the following:
/tmp/ccBliSky.o: In function main':
main.cpp:(.text+0x130): undefined reference toal_show_native_message_box'
main.cpp:(.text+0x160): undefined reference to al_init_font_addon'
main.cpp:(.text+0x164): undefined reference toal_init_ttf_addon'
main.cpp:(.text+0x168): undefined reference to al_init_image_addon'
main.cpp:(.text+0x1a4): undefined reference toal_load_ttf_font'
main.cpp:(.text+0x574): undefined reference to al_draw_textf'
/tmp/ccBMyqom.o: In functionMenuItem::MenuItem()':
MenuItem.cpp:(.text+0xa0): undefined reference to al_load_ttf_font'
/tmp/ccBMyqom.o: In functionMenuItem::~MenuItem()':
MenuItem.cpp:(.text+0x1ac): undefined reference to al_destroy_font'
/tmp/ccBMyqom.o: In functionMenuItem::Draw(GameTime const*)':
MenuItem.cpp:(.text+0x2fc): undefined reference to al_draw_text'
/tmp/ccKXP3ds.o: In functionPlayerShip::SetAudioSample(std::string)':
PlayerShip.cpp:(.text+0x604): undefined reference to al_destroy_sample'
PlayerShip.cpp:(.text+0x64c): undefined reference toal_load_sample'
collect2: error: ld returned 1 exit status
AND NO THIS IS NOT A DUPLICATE OF "What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?"
I know what usually causes most undefined reference errors. This question is specific to the allegro library I'm using.
Here's a bit of code (I'm obviously not going to post the whole game):
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>
#include <iostream>
#include "Game.h"
int main(int argc, char *argv[])
{
const char errorType[] = "Initialization Error";
const int flags = ALLEGRO_MESSAGEBOX_ERROR;
// Initialize Allegro
if (!al_init())
{
const char message[] = "Failed to initialize Allegro Library.";
//al_show_native_message_box(nullptr, "Error", errorType, message, nullptr, flags);
return -1;
}
Game game;
// Initialize a new window for gameplay.
int screenWidth = Game::GetScreenWidth();
int screenHeight = Game::GetScreenHeight();
Thanks in advance for any help!
Alegro divides its functionality across several modules. Your pkg-config line must include all the modules you are using. Try the following:
pkg-config --libs allegro-5 allegro_font-5 allegro_image-5 allegro_ttf-5
I had the same problem with the al_show_native_message_box function and I solved it by adding the following flag:
-lallegro_dialog
I am trying to get OGDF working to see if it is suitable for my project, but I am having trouble with a sample program.
I am trying to compile this example program:
#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/layered/DfsAcyclicSubgraph.h>
using namespace ogdf;
int main()
{
Graph G;
randomSimpleGraph(G, 10, 20);
DfsAcyclicSubgraph DAS;
DAS.callAndReverse(G);
G.writeGML("test.gml");
return 0;
}
using this command:
$g++ -pthread -I ./OGDF/ -L ./OGDF/_release/ -lOGDF test2.cpp
But I get the following error
/tmp/ccbpkfdt.o: In function `main':
test2.cpp:(.text+0x12): undefined reference to `ogdf::Graph::Graph()'
test2.cpp:(.text+0x2e): undefined reference to `ogdf::randomSimpleGraph(ogdf::Graph&, int, int)'
test2.cpp:(.text+0x4e): undefined reference to `ogdf::AcyclicSubgraphModule::callAndReverse(ogdf::Graph&)'
test2.cpp:(.text+0x62): undefined reference to `ogdf::Graph::writeGML(char const*) const'
test2.cpp:(.text+0x7f): undefined reference to `ogdf::Graph::~Graph()'
test2.cpp:(.text+0xa1): undefined reference to `ogdf::Graph::~Graph()'
/tmp/ccbpkfdt.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0xfb): undefined reference to `ogdf::Initialization::Initialization()'
test2.cpp:(.text+0x112): undefined reference to `ogdf::Initialization::~Initialization()'
/tmp/ccbpkfdt.o: In function `ogdf::DfsAcyclicSubgraph::DfsAcyclicSubgraph()':
test2.cpp:(.text._ZN4ogdf18DfsAcyclicSubgraphC2Ev[_ZN4ogdf18DfsAcyclicSubgraphC5Ev]+0x16): undefined reference to `vtable for ogdf::DfsAcyclicSubgraph'
/tmp/ccbpkfdt.o: In function `ogdf::DfsAcyclicSubgraph::~DfsAcyclicSubgraph()':
test2.cpp:(.text._ZN4ogdf18DfsAcyclicSubgraphD2Ev[_ZN4ogdf18DfsAcyclicSubgraphD5Ev]+0xb): undefined reference to `vtable for ogdf::DfsAcyclicSubgraph'
collect2: error: ld returned 1 exit status
I tried compiling hello world, with an include from OGDF, and I still got:
undefined reference to `ogdf::Initialization::Initialization()'
I think I am not linking properly or something?
You have to be very careful in which order you type stuff when linking with a library.
Try putting test2.cpp before -lOGDF instead, like this:
g++ -pthread -I ./OGDF/ -L ./OGDF/_release/ test2.cpp -lOGDF
You must build your program using the -DOGDF_DLL when using OGDF as a shared library.
See here: http://www.ogdf.net/doku.php/tech:defines
I'm lost here. I've been compiling my program successfully on LLVM on my Mac, but when I went to a Linux server and attempted to use g++ to compile, I got a boat load of linker errors.
Here's an excerpt:
/tmp/ccGbgd6T.o: In function `Scene::setBackgroundImage(String)':
Project.cpp:(.text+0x166): undefined reference to `Graph_lib::Image::Image(Point, String, Graph_lib::Suffix::Encoding)'
/tmp/ccGbgd6T.o: In function `Graph_lib::Window::~Window()':
Project.cpp:(.text._ZN9Graph_lib6WindowD2Ev[_ZN9Graph_lib6WindowD5Ev]+0xc): undefined reference to `vtable for Graph_lib::Window'
/tmp/ccGbgd6T.o: In function `Graph_lib::Shape::~Shape()':
Project.cpp:(.text._ZN9Graph_lib5ShapeD2Ev[_ZN9Graph_lib5ShapeD5Ev]+0xb): undefined reference to `vtable for Graph_lib::Shape'
/tmp/ccGbgd6T.o: In function `Graph_lib::Text::Text(Point, String const&)':
Project.cpp:(.text._ZN9Graph_lib4TextC2E5PointRK6String[_ZN9Graph_lib4TextC5E5PointRK6String]+0xe): undefined reference to `Graph_lib::Shape::Shape()'
Project.cpp:(.text._ZN9Graph_lib4TextC2E5PointRK6String[_ZN9Graph_lib4TextC5E5PointRK6String]+0x17): undefined reference to `vtable for Graph_lib::Text'
Project.cpp:(.text._ZN9Graph_lib4TextC2E5PointRK6String[_ZN9Graph_lib4TextC5E5PointRK6String]+0x67): undefined reference to `Graph_lib::Shape::add(Point)'
/tmp/ccGbgd6T.o: In function `Graph_lib::Button::Button(Point, int, int, String const&, void (*)(void*, void*))':
Project.cpp:(.text._ZN9Graph_lib6ButtonC2E5PointiiRK6StringPFvPvS5_E[_ZN9Graph_lib6ButtonC5E5PointiiRK6StringPFvPvS5_E]+0x40): undefined reference to `vtable for Graph_lib::Button'
This scared me, but then I noticed that all the errors are coming from the same class: Graph_lib. Here's an extremely cut out version of what Graph.h looks like: (note, this is not my class)
#ifndef GRAPH_GUARD
#define GRAPH_GUARD 1
#include <...system stuff...>
namespace Graph_lib {
// lots of other classes in here
// this is just one
struct Image : Shape {
Image(Point xy, string file_name, Suffix::Encoding e = Suffix::none);
//rest of class
}
}
What could be going wrong here?
Edit: this is the command I'm using to compile:
g++-4.6 -std=c++0x *.cpp -lfltk -lfltk_images
It appears as though you have forgotten to link your project with Graph.cpp, or whatever file(s) hold the implementations of the Graph_lib class methods.
Looks like your Graph library is missing.
When linking using g++, use -l <Graph lib>
I am a leda-6.3 library user.
I used graphwin to manipulate and display graphs, but I found that references
to graphwin methods are undefined while compiling the code although they
are declared in the LEDA/incl/LEDA/graphics/graphwin.h
So I think it is a problem of object file.
#include <LEDA/graphics/graphwin.h>
#include <LEDA/graph/graph_alg.h>
using namespace leda;
int main()
{
GraphWin gw("LEDA Graph Editor");
node u=gw.new_node(point(100,100));
node v=gw.new_node(point(100,200));
gw.new_edge(u,v);
gw.display();
gw.get_window().read_mouse();
graph& G=gw.get_graph();
G.new_node();
gw.get_window().read_mouse();
gw.update_graph();
gw.get_window().read_mouse();
return 0;
}
compilation: g++ -I$LEDAROOT/incl -L$LEDAROOT gw.cpp -lleda -lX11 -lm -o gw
ERROR :
/tmp/ccVHyRbL.o: In function `main':
gw.cpp:(.text+0x1e): undefined reference to `leda::GraphWin::GraphWin(char const*)'
gw.cpp:(.text+0x58): undefined reference to `leda::GraphWin::new_node(leda::point const&)'
gw.cpp:(.text+0xc6): undefined reference to `leda::GraphWin::new_node(leda::point const&)'
gw.cpp:(.text+0x11c): undefined reference to `leda::GraphWin::new_edge(leda::node_struct*, leda::node_struct*)'
gw.cpp:(.text+0x128): undefined reference to `leda::GraphWin::display()'
gw.cpp:(.text+0x17e): undefined reference to `leda::GraphWin::update_graph()'
collect2: ld returned 1 exit status
Which edition of LEDA are you using?
Please consider that free edition of LEDA does not contain GraphWin.
So, it dos not contain GraphWin libraries, which results to getting such errors while compiling your program.