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.
Related
I wrote a c++ class(files class.h and class.cpp) that I want to use in a c++ application app.cpp from multiple directories on my linux system. Therefore, I moved the class.h and class.cpp files to /usr/local/include/. I tried to compile app.cpp using g++ -o app app.cpp which results in an undefined reference error(probably since g++ does not find the class.cpp file).
I figured out that I can fix this error by either writing the content of class.cpp directly into class.h or by explicitly specifying the location of class.cpp with the extended command g++ -o app app.cpp /usr/local/include/class.cpp. Obviously, both solutions are not satisfactory.
Presumably, someone who does this stuff more regularly(I'm not an computer science student) can give me a quick solution to my problem. Therefore, I ask now instead of investing more hours into searching the web.
Create a static compiled library using g++ -c class.cpp and ar rvs libclass.a class.o(can also create a shared library here)
Compile using g++ -o app app.cpp -lclass
So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).
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.
I've made a little project to get experience with OOP in C++, i'm on Windows (with Cygwin).
The classes are point.h (the base class), point.cpp (implementation of point.h), coloredPoint.h (the derivated class), coloredPoint.cpp (implementation of the derivated class) and main.cpp (whic creates a point object and a coloredPoint object).
To run the main I type
g++ point.cpp coloredPoint.cpp main.cpp -o main
and all goes well! I know that this may could sound stupid...but is this the right way to do it?
For C++ you will want to replace gcc with g++ or add -lstdc++ (if you want to use the std c++ library) to your gcc command line:
gcc point.cpp coloredPoint.cpp main.cpp -o main -lstdc++
or
g++ point.cpp coloredPoint.cpp main.cpp -o main
which links the std c++ library with your compiled code. With g++ you do not have to add this step.
I am having trouble compiling a program I have written. I have two different files with the same includes but only one generates the following error when compiled with g++
/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status
The files I am including in my header are as follows:
#include <google/sparse_hash_map>
using google::sparse_hash_map;
#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using std::priority_queue;
using std::stack;
using std::vector;
using __gnu_cxx::hash_map;
using __gnu_cxx::hash;
using namespace std;
Searching the internet for those two lines hasn't resulted in anything to help me. I would be very grateful for any advice. Thank you
To build two separate programs you need both source files to define main() function.
To build a single program out of two source files - first compile each file with -c options (compile only) - you will get two .o files, then link these files together. Something like this:
$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o
to build binary prog from two source files.
If you need to link with some library, you'll have to point compiler to it's headers with -I and to objects with -L flags, then tell the linker to actually reference the library with -l.
Hope this helps.
You need a main function and you don't have one. If you do have a main function, show more code please.
It looks like main is not defined. Do you have one defined for your second program? Can you post more details about the source body that fails to link?