Problems setting up Ncurses and C++ - c++

I'm fairly new to C++ and I'm trying to set up ncurses, but I can't get it to work.
Here's the code:
#include <iostream>
#include <string>
#include <ncurses.h>
int main(){
initscr();
printw("Hello World !!!");
refresh();
getch();
endwin();
return 0;
}
With this file I get 'undefined reference' errors
And here is the makefile:
main.o: main.cpp ncurses.h
g++ main.cpp -o crawler -lncurses
The error I get with the makefile is :
make: *** No rule to make target `ncurses.h', needed by `main.o'. Stop.
Thanks for your help!
Note: I am using Ubuntu 12.04 with Geany and g++

You should remove ncurses.h dependency from Makefile. You Makefile should look like this:
main.o: main.cpp
g++ main.cpp -o crawler -lncurses
make tries to find ncurses.h in current working directory, but it is not available there. So make indicates error.
Also, there is no need of iostream and string headers in your code, because string header is included by iostream and you are not using any functions from both of headers.

Related

Gdiplus not working, error "PROPID has not been declared"

I'm a beginner when it comes to C++. I'm trying to use GDI+ and I'm following this tutorial from Microsoft: Drawing a Line
I'm building the app with the following commands, but all with the same result:
g++ main.cpp window.cpp -o app.exe
g++ main.cpp window.cpp -libgdiplus -o app.exe
g++ main.cpp window.cpp -libgdiplus -lgdi32 -o app.exe
It gives me a lot of errors inside of gdiplusimpl.h, almost all of which are referring to PROPID not being declared or not naming a type.
This is the way I refer to the library in my cpp files:
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "Gdiplus.lib")
What can I do to be able to build the app?
While following the tutorial, I missed an import (objidl.h). After adding it in the proper place, Almost all errors went away. It still didn't compile though, and was giving errors about undefined GDI+ functions. However, that was because I was using the wrong command. By running this command it compiled correctly:
g++ main.cpp window.cpp -lgdiplus -o app.exe
The difference was a simple typo: lgdiplus instead of libgdiplus.

Problems including GLFW header in c++ program

I need to include GLFW header in my c++ program. The installation was fine and the GLFW folder exists in my usr\include folder and g++ does look for header files in that folder. Despite that it throws an error telling me that the GLFW directory doesnt exist.
I am using sublime text as my editor and IDE and my system is Ubuntu-20.04
FOllowing is the code, the terminal command i used to compile and the error message i encountered:
#include <GLFW\glfw3native.h>
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "All DOne!!" << std::endl;
return 0;
}
g++ -g -o bin/debug/main src/*.cpp -x64 -std=c++17 -Wall -I -include -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread && ./bin/debug.main
src/main.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
1 | #include <GLFW\glfw3.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I cannot tell where the problem lies. please help.
and my system is Ubuntu-20.04
On Linux, the path delimiter is /, not \, so
#include <GLFW/glfw3native.h>
Note that while windows primarily uses backslash, it also accepts the forwad slash, so always using / is also the best option for cross-platform programming.

compile and run c++ program with own header files in linux

This is my first go at making my own header file. I am trying to make a simple Hello World program in C++ on Ubuntu. made 3 files as follows :
//hello.h file
#ifndef HELLO_H
#define HELLO_H
//my own code
void hello();
#endif
//hello.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
void hello()
{
cout << "This line is printed from header.";
}
//main.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
cout << "in main" << endl << endl;
hello();
return 0;
}
I've tried
g++ -I /home/Desktop/hello/ -c hello.cpp -o hello.o
to compile header file and this command worked.
then, while doing
g++ -o main main.cpp
I am ending up with following error:
/tmp/ccb0DwHP.o: In function `main':
main.cpp:(.text+0x2e): undefined reference to `hello()'
collect2: error: ld returned 1 exit status
Please suggest whether changes need to be made in any file or in any command in the terminal?
thank you
You don't link to hello.o in the command below:
g++ -o main main.cpp
Try this:
g++ -o main main.cpp hello.o
Or for such simple program, just issue the command below:
g++ -o main main.cpp hello.cpp
For ease of use, create a makefile, then you just run make:
make
A simple makefile:
helloprogram: hello.h hello.cpp main.cpp
g++ -o helloprogram main.cpp hello.cpp
clean:
rm helloprogram
Put hello.h in Path2Hello;
g++ -o main -I Path2Hello main.cpp hello.cpp
ps: -I option to specify an alternate include directory (for header files).
To compile and run a C language program, you need a C compiler. To setup a C language compiler in your Computer/laptop, there are two ways:
Download a full fledged IDE like Turbo C or Microsoft Visual C++, which comes along with a C language compiler.
Or, you use any text editor to edit the program files and download the C compiler separately.

Error compiling source file and header file together in C++

This is not actual code i am working on but sample code i had written to understand what i am doing wrong. So i have three files main.cpp, favourite.cpp and favourite.h. I am trying to compile main.cpp but get some weird error.
// main.cpp File
#include <iostream>
#include "favourite.h"
using namespace std;
int main()
{
favNum(12);
}
// favourite.cpp File
#include "favourite.h"
#include <iostream>
using namespace std;
void favNum(int num)
{
cout << "My Favourate number is " << num << endl;
}
// favourite.h File
#ifndef FAVOURITE_H
#define FAVOURITE_H
void favNum(int num);
#endif
This all files are in same folder and i am compiling it normally like g++ main.cpp I am not sure if i need to compile it diffrently as i am using custom header files.
If you say g++ main.cpp and this is your whole command line, the error is a linker error that it can't find favNum, right? In that case, try:
g++ main.cpp favourite.cpp
or split compilation and linking:
g++ -c main.cpp -o main.o
g++ -c favourite.cpp -o favourite.o
g++ main.o favourite.o
Where -c means: Compile only, no linking and -ofilename is required because you want to write the output to two different object files to link them with the last command.
You might also add additional flag, the most important ones are:
-Wall -Wextra -O3
Oh I guess I see the error although you should have included it in your question.
When compiling multiple source files you need to list them all on the GCC command line. Or you can use a Makefile.
So you could do this:
g++ favourite.cpp main.cpp
Or you could write a Makefile like this:
all: program
program: main.o favourite.o
And then just type:
make

Multi-file C++ compilation

(hopefully) quick question that I can't find the answer to:
I have been given a brief assignment in C++. We are to write a 3-file program. There will be a function file, a header file, and a driver file. Here's what I've got so far:
Header (test.h):
#include <iostream>
using namespace std;
#ifndef TEST_H
#define TEST_H
int foo (int bar);
#endif
Function (test.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int foo (int bar){
bar++;
}
Driver (drive.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int main(){
int x = foo(2);
cout << x << endl;
return x;
}
When I try to compile drive.cpp, I get the following error:
drive.cpp:(.text+0xe): undefined reference to `foo(int)'
So...what am I doing wrong?
For a small project like this, simply compile all .cpp files at once:
g++ main.cpp driver.cpp
For a larger project, you separate the compile and link steps:
compile:
g++ -c main.cpp -o main.o
g++ -c driver.cpp -o driver.o
link:
g++ main.o driver.o
Or rather, you'd have a makefile or IDE do this for you.
In drive.cpp, instead of
#include <test.h>
make it
#include "test.h"
This is the variant of #include syntax that is used for header files of your own program (not system header files). When you use this version the preprocessor searches for include files in the following order:
In the same directory as the file that contains the #include statement.
In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
You need to do one of two things:
Compile all the files at once
# replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o driver.exe main.cpp driver.cpp
Compile all the files to object files and then link the object files:
# again, replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o main.o -c main.cpp
g++ -Wall -ggdb -o driver.o -c driver.cpp
g++ -Wall -ggdb -o driver.exe main.o driver.o
As a side note, you should probably change
#include <test.h>
to
#include "test.h"
and putting "using namespace std;" in a header file is going to cause you copious grief later on.
in test.cpp, change the return line to this:
return bar++;