Using eclipse CDT without creating a project - c++

I am currently learning c++.My first language was python.I am used to coding in eclipse in pydev.
I learn a language by writing a lot of code snippets and use the debugger extensively to understand what is actually happening.
Now,I downloaded CDT for eclipse,since I am used to the interface.
But now,I am forced to create a project for every c++ file I write. I want a way to easily execute the files(very small,max 300 lines) in fast manner,and without creating new projects again and again.Any ideas?
Currently I use eclipse like a text editor,and use g++ for compiling the files.

I suggest you create one project containing all your files (assuming each one has its own main function), and inside this project, create one 'Build configuration' per program by using :
Project > Properties > C/C++ Build > Configuration > Manage... > New
To avoid multiple definition of main in each 'Build configuration', you will have to exclude each cpp file from the 'Build configurations' it doesn't belong to. For this, in the Navigator view :
Right click on the cpp file > Properties > C/C++ Build, and check 'Exclude from build' for each 'Build configuration' the source file does not belong to.
When everything compiles fine, you will be able to run or debug each program separately inside Eclipse by selecting the matching 'Build configuration'.
More info in this answer :
What files belong to a build target in CDT managed build?
I hope this will help!

Related

Compile Single file in a project in netbeans

I am using NetBeans Ide 8.2 on ubuntu 16
I am learning C and C++ programming hence the program i make are not related and when I try to create multiple file in a project the include multiple main() function which return error
error: multiple main function found
I want to create separate file for each program but I don't want to create separate project for all the program.
please suggest me a way & I am sorry for my language , I hope i am able to make you understand my problem.
To have multiple main files in a single Netbeans project:
Under the Projects tab in the left, double-click on your project to show its contents.
Double-click on the Source Files folder to show its contents
Right-click on the earlier main file (most-likely main.cpp) and select Properties.
Under the Item Configuration tab, check the Excluded from build option and click on Ok.
Right-click on the Source Files folder, select New and click on C++ Main File. This will create a new main file in the same project.
Now, when you build and run your project, it will compile the new main file, and the old one will be ignored.
Tip: It is a good idea to give unique names to the main files, to avoid name clashes.

How to create a C++ project in Eclipse CDT using existing source files and an existing CMakeLists.txt?

The title says it all: I have source files and a CMakeLists.txt for an existing C++ project. Now I want to create a project in Eclipse CDT using those files.
I want to be able to build the project with the "build" button from within Eclipse once I'm done, and I want to be able to use GDB.
I've used the CMake option to create Eclipse projects before, but it didn't always work flawlessly and I was told the CMake generator for Eclipse is supposedly outdated.
I know I could just create an empty project and then copy the source files into it, but what about the CMakeLists.txt? I want Eclipse to know how to build the project.
I'm new to Eclipse and there are like fifty ways to start a new project with or without existing source files, so some guidance would be greatly appreciated.

How to exclude a source file from being build in eclipse (C++)?

I am trying to follow some insane complicated steps in order to be able to create C++ unit-tests for C++ code in eclipse (I am utterly new to eclipse). These steps state to exclude some source files from using in the built - which makes sense to me. However, in my eclipse I do not see any Exclude from built option when right-clicking on the source file. Also, neither this answer nor this answer do work, as I do not see the mentioned options in the list of things when I right-click on the source file.
So how to exclude a source file from a built?
P.S. The Version of eclipse seems to be Luna 4.4.0.
(Or, even better: If anyone knows an easier way to set up eclipse with ANY unit-testing framework in a SIMPLE way,- or knows a different Linux framework to start developing C++ projects with unit-testing right away without trying to set up things in unknown and complicated ways for weeks before starting to actually code something - ideas are VERY VERY welcome...)
I ran into a similar problem.
The C/C++ perspective of Eclipse Luna (4.4) is missing the contextual menu "Exclude from build".
I solve my problem by using the java perpective:
Window -> Open Perspective -> Other... -> Java(default)
Navigate to the file/folder you want to exclude
Right Click -> Resource -> Exclude from build
Then you can switch back to the C/C++ CDT perspective
Project Properties> C/C++ General> Path and Symbols
Tab Source Location
Select the source folder
Click the Edit Filter button
Add the file you want to exclude
I've tried this and it worked on Eclipse:
Right Click the file > Properties ... > Resource > Attributes : Derived.
(Eclipse Indigo)
Hope that helps.
I am using MARS eclipse version.
You have to create a build configuration for each component you are building.
Here is how to do that: multiple_execs
Then for each source file that you do not want included in that build; you right click on that source file and then select Resource Configuration and select Exclude From Build. Then you check the box of the build configuration you created above that you do not want the source a part of.
Here is YouTube video showing this: exclude files from a build

Eclipse indexing not working

I am using CDT in Eclipse 3.5.2 Galileo in Ubuntu.
My eclipse's indexer doesn't work at times. By that I mean when I ctrl click a function it says Could not find symbol in index. Also, F3 button click results in the same error message. Actually most of the places it is not working and works in very few places.
I have seen some other versions of Eclipse where under File menu a menu item listed for Indexing. Like File->Indexer->Rebuild.... But in my File menu there is no such menu item for the Indexer.
Will the only way be to use some other release of Eclipse or am missing something? Any pointers regarding this would be helpful.
In my case nothing has worked and at last I did the following:
1) Close the project
2) Delete the project(do not check from disk option)
3) create new project with existing code(use the deleted project path)
4) Successfully indexed my c++ code
To add paths containing code to parse, follow these steps :
1. Right click on the project
2. Select Properties
3. Go to C/C++ General
4. Go to Path and Symbols
5. If the paths are missing, add paths.
To re-parse the code follow these steps :
1. Right click on the project
2. Select Index
3. Rebuild
If this doesn't work, then you are out of luck. c++ is very difficult language to parse.
If your indexer is stuck and it hangs for infinite time, you can delete the indexer related files of your project from .metadata. Make sure eclipse is not running in the meanwhile.
The .metadata directory is located as a hidden folder in the path of your workspace.
There you can navigate to:
<workspace_path>/.metadata/.plugins/org.eclipse.core.runtime/.settings/
Inside the .settings/ directory there are .prefs files which store the preferences of each project individually.
For example: org.eclipse.cdt.core.prj-test-project.prefs stores the preferences of the project test-project
Open this file with the text editor and remove all the lines starting with indexer. This will reset the indexer settings for this specific project to the default.
Alternatively, you can remove all the lines starting with indexer from org.eclipse.cdt.core.prefs; doing this will reset the indexer settings for the whole workspace.
I believe you can clear the index by running:
rm .metadata/.plugins/org.eclipse.jdt.core/*.index
And then restarting Eclipse. It will then rebuild its index.
This worked for me in a Java project, but I imagine its the same index for all (most?) project types.
Right click on the project.
Select New->Convert to a C++ Project.
Select your configuration. And then Next
Indexer will start indexing all the files.
For me this worked:
Right click and close the project
Open the project again
Right click and select Index->Rebuild
I hope it helps anybody.
You can try rebuilding the index.
Right-click on the project in Project View. Then Index -> Rebuild.
Here is the solution I used after Project->C++ Index->Rebuild failed. This trick avoid deleting your current project :
1) Copy the current project (Right click->Copy from Project Explorer)
2) Paste it (Right click->Paste from Project Explorer) and give the copy another name
3) The indexer should now start and index both projects
4) When indexation is complete, you can delete the copy
PS: Make sure your original project is an actual C++ Project or this will obviously not work.
Convert a project to C or C++ nature:
Right click on project: New >  Convert to C/C++ Project
In "Convert to C or C++": Choose C/C++ Project
In "Project options": Check "Specify Project Type"
...Project Type: Choose "Makefile project"
...Toolchains: Gross GCC
I was having the same problem and it turns out that the index folder was owned by root (I must have run Eclipse as a root once for some reason) I reclaimed the project folder using sudo chown -R username projectfolderpath and problem solved.
Execute eclipse.exe -clean -refresh where the eclipse is installed.
Create a new C/C++ Project in Eclipse with Given Source code folder path. (Not with New Project based on existing workspace)
Above steps worked for me.
For me it was issue that when creating a project indexer was chosen as none. Deleted the project and while creating the project again I chose the indexer options as per my need "LINUX_GCC", then it worked for me
Just came across the same problem in my C++ ARM Cross GCC project. I'm developing on Windows, using Makefile and an Ubuntu Docker image with GNU ARM Embedded Toolchain to build the project.
The solution to get rid of the unresolved symbols, in my case, could be divided into two parts:
The CDT Arm Cross GCC Built-in Compiler Settings didn't have the path to the compiler.
In the Project Properties -> C/C++ General -> Preprocessor Include Paths -> Providers(tab) the path to the compiler must be provided.
Using the GNU ARM Embedded Toolchain, the entry for the Command to get compiler specs: I currently use is:
C:\GNU_Arm_Embedded_Toolchain\9-2020-q2-update\bin\arm-none-eabi-g++.exe ${FLAGS} ${cross_toolchain_flags} -E -P -v -dD "${INPUTS}"
Note: Yes, I am having installed the same toolchain on my Windows machine as I'm using in the Docker image.
To verify, that the call is correct, you can tick the Allocate console in the Console View, which will provide hlepful output to the Console. Also, in the Entries tab, when you unroll the CDT Arm Cross GCC Built-in Compiler Settings you should see the includes and symbols included.
Missing symbol in the Project Properties -> C/C++ General -> Paths and Symbols menu.
As I have my defines in the Makefile, I forgot to add the define of my MCU also here.
Hope this helps to someone! :)
For the most part, the other answers tackle the situation wherein one "had" a working index earlier and things have gone astray for some reason.
On the other hand, I faced a problem wherein I needed to install everything from scratch (eclipse, workspace, c++ project etc.) and the indexing just wouldn't work. I finally found the solution here: https://www.eclipse.org/forums/index.php/t/1109004/
The key inputs from the above page are:
You need to setup include paths for the Indexer just as you would need
to for the compiler. The Indexer and compiler are completely independent.
The Indexer include paths are set by
Project -->Properties --> C/C++ General --> Preprocessor Include Paths, Macros etc. --> Entries tab --> CDT UserSettings Entries ...
and
You could try using
Project --> Properties --> C/C++ General --> Paths and Symbols
That's supposed to merge telling the compiler and Indexer
in one place.
However, this latter part of the suggestion did not work for me.

How Do I Use Eclipse to Debug a C++ Program on Linux?

I don't use Eclipse as an IDE, and have no interest in doing so. However, I do like its source-level debugging.
Is there any way I can use it to debug a C++ Linux app without going through the ritual of creating a project? (In effect, can I just use it like a frontend to gdb?)
If not, what are the steps I need to follow to create a project that I can use to just debug an existing C++ program that is built using Makefiles or other tools (SCons, CMake, etc.). I don't want to be able to "develop" in Eclipse; all I need to do is debug.
Take a look at this question. Create a C/C++-project, use your project's source directory as project directory, select to use the external builder, and change "make" to whatever tool you want.
The tricky part is to get the indexer to work correctly and find all your header files.
EDIT: CMake 2.6.x has support for generating CDT project files, which might be a more straightforward solution.
I don't know if this has changed in the 4+ years since the question was posted, but there's a much easier way to do this. I'm on Eclipse Luna (4.4.2).
> eclipse&
then
File > Import > C/C++ > C/C++ Executable > Next > browse to executable > Next > choose a project name > Finish
No other project setup required, no source paths (which should be in the object code). Just like running gdb/insight/etc. Almost makes it worth installing Java.
Configuration for debugging in Eclipse.
In eclipse,
Go to Window->preferences
A popup will appear then select C/C++ , click on drop down arrow ,then select Debug and click on drop down arrow.
Select Source Lookup Path and then click on Add.
After clicking on Add, click on Path Mapping and then click on OK.
Specify the mapping path name and then click on Add .
In compilation path select Cygwin path (need to install) and then click ok .
In debug option,click on source Lookup Path and select Path Mapping:Project source and click on apply and then ok.