How to use eclipse to build C++ applications

2019-03-28 11:14发布

I have downloaded Eclipse Juno for C++ from here. I am trying to build a simple hello world program (mostly just to test out using eclipse for C++), but I can't get it to build and run. Here's my code:

#include <iostream>
using namespace std;

int main() {
    cout << "hello" << endl;
    return 0;
}

The Problems tab shows

make: *** No rule to make target `all'.

I can only assume this is happening because eclipse is not configured to find my compiler (g++ for archlinux), but I can't figure out how to fix it. Ideally I want to be able to program in C++ with Eclipse as easily as you can in Java (i.e. write code, build and run).

2条回答
【Aperson】
2楼-- · 2019-03-28 11:39

A Makefile is a file that serves as a "map" to build a project from the command line (or using an IDE like in your case). It is good practice to use a Makefile to compile complex projects composed of many files, since the compilation can be done with or without the IDE (Eclipse in your case). To compile your project outside Eclipse, just type "make" from the command line.

In your case, somehow you have selected the option to compile using a makefile (this option in my opinion is the best).

To solve your problem from this point (without having to re-create the project), just add a blank file to your project:

File-> New-> Other
Expand "General" and select "File"

Name it "Makefile" (it is important to put this name) (I assume that your source code is stored in a file named "main.cpp" and that your executable will be called "hello") (I assume also that the g++ is installed on your system, as some Linux distributions have gcc preinstalled only)

Edit the file "Makefile" (in the same Eclipse if you like) with the following: (it is important to respect the tabs!)

all: main.o
     g++ -o hello main.o

main.o: main.cpp
     g++ -c main.cpp

clean:
     rm *.o

Now, when you compile your project, you should see something like this: (in the "console" tab)

make all
g++ -c main.cpp
g++ -o hello main.o

This is the simplest Makefile you can create for your project. There is plenty of help available on the web about the syntax of the Makefile for much more complex builds.

查看更多
淡お忘
3楼-- · 2019-03-28 11:44

follow this: create new c++ project. Than in project type chose Executable - > Hello world.

查看更多
登录 后发表回答