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
Related
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>
I've been trying to debug and compile my C++ with Xlib code in CodeLite for a while now, but I can't seem to pass the -lX11 argument to the gcc compiler in CodeLite. How would I go about doing this (On Linux Ubuntu 16.04 LTS)?
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
int main(int argc, char **argv){
Display *display;
XEvent event;
Window win;
int screen;
bool running = true;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
win = XCreateSimpleWindow(display,XRootWindow(display,screen),100,100,500,300,1,XBlackPixel(display,screen),XWhitePixel(display,screen));
XMapWindow(display,win);
while(running){
XNextEvent(display,&event);
}
}
Console Output:
----------Building project:[ Xlib - Debug ]----------
make[1]: Entering directory '/home/owner/Documents/C/Xlib'
/usr/bin/g++ -o ./Debug/Xlib #"Xlib.txt" -L.
./Debug/main.cpp.o: In function `main':
/home/owner/Documents/C/Xlib/main.cpp:14: undefined reference to `XOpenDisplay'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XWhitePixel'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XBlackPixel'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XRootWindow'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XCreateSimpleWindow'
/home/owner/Documents/C/Xlib/main.cpp:17: undefined reference to `XMapWindow'
/home/owner/Documents/C/Xlib/main.cpp:20: undefined reference to `XNextEvent'
Thanks in advance.
I have created a simple C++ application. I can compile it, and it works fine. But now I need to load the library dynamically, and I have added dlfnc.h to my project and added some more code:
#include <iostream>
#include <dlfcn.h>
void *mylib;
int eret;
using namespace std;
int main() {
mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
eret = dlclose(mylib);
cout << "!!!Hello, World!!!" << endl; // Prints !!!Hello, World!!!
return 0;
}
Compiling:
cd ~/workspace/LinuxGcc/src
g++ LinuxGcc.cpp
And I got a compilation error:
/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
dlfcn.h exist in /usr/include/.
Where is the problem?
From dlopen(3):
Link with -ldl.
so
g++ LinuxGcc.cpp -ldl
will be OK.
The solution is very simple. Add the -ldl flag for linking.
In case of the Bazel build system, linkopts = ['-ldl'].
I'm trying to compile my C++ code using Magick++ library to manipulate images in a distributed way using openMPI and I get some errors when I try to compile it.
This is my code:
#include "mpi.h"
#include <stdio.h>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv){
int rank, numtask;
InitializeMagick(*argv);
Image image;
try {
// Read a file into image object
image.read( "test_image.jpg" );
image.type( GrayscaleType );
Blob blob;
image.magick( "JPEG" ); // Set JPEG output format
image.write( &blob );
}
catch( Exception &error_ ){
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
//Now in the "distributed enviroment" I just print an hello world to test it.
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtask);
cout<<"HelloWorld\n";
MPI_Finalize();
}
this is the command that I type on the shell
mpiCC openmpi_project.cc -o openmpi_project
and this is the output of the shell
openmpi_project.cc:(.text+0x1d): undefined reference to
"Magick::InitializeMagick(char const*)"
openmpi_project.cc:(.text+0x29): undefined reference to
"Magick::Image::Image()"
openmpi_project.cc:(.text+0x5d): undefined reference to
"Magick::Image::read(std::string const&)"
openmpi_project.cc:(.text+0x86): undefined reference to
"Magick::Image::type(MagickCore::ImageType)"
openmpi_project.cc:(.text+0x92): rundefined reference to
"Magick::Blob::Blob()"
openmpi_project.cc:(.text+0xc6): undefined reference to
"Magick::Image::magick(std::string const&)"
openmpi_project.cc:(.text+0xf1): undefined reference to
"Magick::Image::write(Magick::Blob*)"
openmpi_project.cc:(.text+0xfd): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x158): undefined reference to
"Magick::Image::~Image()"
openmpi_project.cc:(.text+0x1d3): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x261): undefined reference to
"Magick::Image::~Image()"
/tmp/ccqFzUdy.o:(.gcc_except_table+0x58): undefined reference to
"typeinfo for Magick::Exception"
ImageMagick ships with config utilities. For Magick++ this utility is Magick++-config. See the Usage sub-section under the API docs.
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
$(CC) $CXXFLAGS openmpi_project.cc $LDFLAGS -o openmpi_project
Jump over to the MPI compiling/linking docs, and integrate Magick++'s additional flags to mpiCC
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
mpiCC --with-wrapper-cxxflags=$CXXFLAGS openmpi_project.cc \
--with-wrapper-ldflags=$LDFLAGS -o openmpi_project
Here's my code. I'm just testing Boost::process so I'll be able to use it if/when I need to. I don't know why I'm getting the linking error that I am getting. I'm a rather novice C++ programmer. I know the concepts, but I make frequent errors in practice and am bad at debugging. I appreciate any help I can get with this.
#include<iostream>
#include<boost/process.hpp>
#include<boost/iostreams/device/file_descriptor.hpp>
namespace bp = ::boost::process;
namespace bpi = ::boost::process::initializers;
namespace bio = ::boost::iostreams;
int main(int argc, char *argv[])
{
bp::pipe p = bp::create_pipe();
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
bp::execute(
bpi::run_exe("/usr/bin/ls"),
bpi::bind_stdout(sink)
);
return(0);
}
And here is my error…
/tmp/cc7cmrV8.o: In function `main':
test.cpp:(.text+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(int, boost::iostreams::file_descriptor_flags)'
/tmp/cc7cmrV8.o: In function `boost::process::posix::initializers::bind_stdout::bind_stdout(boost::iostreams::file_descriptor_sink const&)':
test.cpp:(.text._ZN5boost7process5posix12initializers11bind_stdoutC2ERKNS_9iostreams20file_descriptor_sinkE[_ZN5boost7process5posix12initializers11bind_stdoutC5ERKNS_9iostreams20file_descriptor_sinkE]+0x2b): undefined reference to `boost::iostreams::file_descriptor_sink::file_descriptor_sink(boost::iostreams::file_descriptor_sink const&)'
/tmp/cc7cmrV8.o: In function `void boost::process::posix::initializers::bind_stdout::on_exec_setup<boost::process::posix::executor>(boost::process::posix::executor&) const':
test.cpp:(.text._ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_[_ZNK5boost7process5posix12initializers11bind_stdout13on_exec_setupINS1_8executorEEEvRT_]+0x18): undefined reference to `boost::iostreams::file_descriptor::handle() const'
collect2: error: ld returned 1 exit status
Platform: Linux 64-bit
Boost: 1.55 (installed via pacman)
Boost::process: 0.5
Compile command: g++ -Wall test.cpp -o spegh.elf -lboost_system
A simple search threw me at -This-.
Seeing you posted your compile command, I'm guessing you are simply missing -lboost_iostreams in your linker settings.