Graphics.h c++ undefined reference to various functions like line(),initgraph() [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 11 months ago.
#include<graphics.h>
#include<stdio.h>
main()
{
int gd=DETECT,gm;
int i,x,y;
initgraph(&gd,&gm,"");
line(0,0,640,0);
line(0,0,0,480);
line(639,0,639,480);
line(639,479,0,479);
for(i=0;i<=1000;i++)
{
x=rand()%639;
y=rand()%480;
putpixel(x,y,15);
}
getch();
closegraph();
}
The Following is a Basic Graphic Program,It Shows the Errors as
undefined reference to 'initgraph'
undefined reference to 'closegraph'
undefined reference to 'line'[4 times]
undefined reference to 'putpixel'
Compiler : CodeBlocks; Language:c++;
I Have Copied the graphics.h and winbgim.h in include folder and the libbgi.a in the lib folder also i have linked all the libraries required to be linked. Please Help.

Change
initgraph(&gd,&gm,"");
to
initgraph(&gd,&gm,NULL);
for compiling:
g++ -o filename filename.cpp -lgraph
to execute:
./filename

The functions in graphics.h are only supported by old ancient Turbo C and Turbo C++ compilers. Those functions can not be used by any modern 32-bit compiler. You can either get a copy of that old MS-DOS compiler or use win32 api GDI functions or get one of several graphics libraries such as OpenGL and QT.

Related

Undefined reference to linker error when using namespaces in headers in c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.
This is the simplest test I could put together that recreates the issue I'm having.
Compiler: g++ (Debian 8.3.0-6) 8.3.0
hntest.h
namespace hntest
{
void pewpew();
}
hntest.cc
#include <iostream>
#include "hntest.h"
namespace hntest
{
void pewpew()
{
std::cout << "pew pew pew!!!" << std::endl;
}
}
hntestmain.cc
#include "hntest.h"
int main(int argc, char* argv[])
{
hntest::pewpew();
}
I'm attempting to compile with:
g++ -lstdc++ hntestmain.cc -o hntestmain
And I get the following linker error:
hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status
I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.
What am I missing?
You are not actually compiling the cpp file that has the definition of pewpew.
Try:
g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.
For much greater detail see the answer here which is specific for your case of linker error.

opengl with glfw not linking with imgui c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I am learning opengl and decided to use imgui to an working existing opengl cmake project by adding following files in the project:
target_sources(Application PRIVATE
vendor/include/imgui/imgui.cpp
vendor/include/imgui/imgui_draw.cpp
vendor/include/imgui/imgui_impl_glfw.cpp
vendor/include/imgui/imgui_impl_opengl3.cpp
vendor/include/imgui/imgui_widgets.cpp
vendor/glad/glad.c
vendor/include/stb_image/stb_image.c
src/Indexbuffer.cpp
src/Shader.cpp
src/Texture.cpp
src/Vertexarray.cpp
src/Vertexbuffer.cpp
)
while this works fine on windows but when i build it in linux linker shows error saying:
in function `ImGui_ImplOpenGL3_Init(char const*)':
imgui_impl_opengl3.cpp:148: undefined reference to `glGetIntegerv'
imgui_impl_opengl3.cpp:230: undefined reference to `glEnable'
imgui_impl_opengl3.cpp:231: undefined reference to `__glewBlendEquation'
all other glfw and opengl functions are working in my application.cpp it only breaks when i add imgui files to the project.
the problme is not imgui but the lib glfw and glew. so it seem you have not link the libs

Undefined reference error in C [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am using C library antlr3c. I installed the library using: sudo apt-get install libantlr3c-dev
#include "antlr3defs.h"
..
string DBparser::sparqlParser(const string& _sparql, SPARQLquery& _sparql_query)
{
pANTLR3_INPUT_STREAM input;
pSparqlLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
pSparqlParser parser;
input = antlr3StringStreamNew((ANTLR3_UINT8 *)(_sparql.c_str()),ANTLR3_ENC_UTF8,_sparql.length(),(ANTLR3_UINT8 *)"QueryString");
}
When I run the program containing the above fragment I get the error:
NetBeansProjects/gstore/Parser/DBparser.cpp:25: undefined reference to `antlr3StringStreamNew'
I am not getting how to resolve this error as antlr3StringStreamNew is indeed declared in antlr3defs.h. Although I am unable to find its definition.
If this related to incompatibility with version 3.4 of antlr3c (as I have installed version 3.2). If this is indeed the case then, is there any alternate function in antlr3c version 3.4 by which I may achieve the same functionality.
Keith is right - the linker you are using is expecting different symbols due to C++ name mangling. Thus, though your code will compile, at the link stage it fails with that error.
If you surround your header include like so, the linker should find the symbols:
extern "C" {
#include "antlr3defs.h"
}
The result is that your code compiles with references to C-style function names, allowing the linker to match them up with corresponding symbols in the object files of the antlr3 library.

Code::Blocks winmain#16 after creating a class [duplicate]

This question already has answers here:
undefined reference to `WinMain#16'
(7 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I always get this error 'Undefined reference to WinMain#16' after creating a class in Code::Blocks. I have to restart it to make the program works.
Why ?
Thank you!
If you have only one file - your class - and you trying to compile it, you will get this error because file don't have int main() function. It's required by linker to create executable (start point of program).
If you have project with classes, you must have one main function, for example in main.cpp file :)
Also, check that you selected a Console application - GUI (Windows application) neeed WinMain function instead of classical main.
Of course, this is about normal program - library have other requirements.

How to use GLUT in DEV C++? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
The compiler throws errors like:
" [Linker error] main.o:main.cpp:(.text+0x972): undefined reference to `_imp__glutReshapeFunc#4' "
Do you know how to use GLUT in Dev C++?
DevC++ is seriously outdated. I recommend using Codeblocks instead.
The error line you quoted simply indicated, that the linker is missing the functions of the GLUT library. Including the headers is not enough (they just provide the compiler with sort of an index). But the linker still needs to be told which libraries to actually link against.