Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Newbi Question regarding complete C enviorment
Newbi Question regarding complete C enviorment [message #1707207] Thu, 03 September 2015 06:29 Go to next message
Tom Cat is currently offline Tom CatFriend
Messages: 5
Registered: September 2015
Junior Member
HI ,

I got a C SRC code that i want to have it build in eclipse environment , this SRC was build with in a different environment that uses some kind of batch files to build them all.
I want to ramp up a complete eclipse development environment to build this entire SRC code , and i have no ideas where to begin at all.

What are the steps that i need to do ?

Thanks.
Tom
Re: Newbi Question regarding complete C enviorment [message #1707243 is a reply to message #1707207] Thu, 03 September 2015 08:52 Go to previous messageGo to next message
Fadi J is currently offline Fadi JFriend
Messages: 11
Registered: June 2013
Junior Member
to the best of my knowledge there is two solutions.
1- you can create an eclipse headless plug-in and then run eclipse from the command line. Notice that when running eclipse from the command line you still needs to open an workspace and create a C project and link its source folder with the SRC code location. If you wish I can provide you with an example.
2- you can run CDT outside eclipse. To do that you needs those jar files (any version is ok):
org.eclipse.cdt.core_5.9.1.201502131403.jar
org.eclipse.cdt.ui_5.9.0.201502131403.jar
org.eclipse.core.contenttype_3.4.200.v20140207-1251.jar
org.eclipse.core.jobs_3.6.1.v20141014-1248.jar
org.eclipse.core.resources_3.9.1.v20140825-1431.jar
org.eclipse.core.runtime_3.10.0.v20140318-2214.jar
org.eclipse.equinox.common_3.6.200.v20130402-1505.jar
org.eclipse.equinox.preferences_3.5.200.v20140224-1527.jar
org.eclipse.osgi_3.10.2.v20150203-1939.jar

but in this case you have to manage the included files manually. An example:

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import org.eclipse.cdt.codan.core.cxx.internal.model.cfg.ControlFlowGraphBuilder;
import org.eclipse.cdt.codan.core.cxx.internal.model.cfg.CxxControlFlowGraph;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.cxx.model.CxxModelsCache;
import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext;
import org.eclipse.cdt.codan.core.model.cfg.IBasicBlock;
import org.eclipse.cdt.codan.core.model.cfg.ICfgData;
import org.eclipse.cdt.codan.core.model.cfg.IControlFlowGraph;
import org.eclipse.cdt.codan.internal.core.CheckerInvocationContext;
import org.eclipse.cdt.codan.internal.core.cfg.ControlFlowGraph;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.util.ASTPrinter;
import org.eclipse.cdt.internal.core.dom.IIncludeFileResolutionHeuristics;
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.index.CIndex;
import org.eclipse.cdt.internal.core.parser.IMacroDictionary;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContentProvider;
import org.eclipse.cdt.ui.tests.DOMAST.CPopulateASTViewAction;
import org.eclipse.cdt.ui.tests.DOMAST.DOMASTNodeLeaf;
import org.eclipse.cdt.ui.tests.DOMAST.DOMASTNodeParent;
import org.eclipse.cdt.ui.tests.DOMAST.IPopulateDOMASTAction;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;

import de.tuc.ifi.sse.ipsse.codeanalyzer.util.Util;


public class Test {

public static IASTTranslationUnit ast;

public static void main(String[] args) {
ILanguage lang = GCCLanguage.getDefault();

FileContent fc = FileContent.createForExternalFileLocation("Test c.c");

try {

InternalFileContentProvider crf = new InternalFileContentProvider() {

@Override
public InternalFileContent getContentForInclusion(IIndexFileLocation ifl, String astPath) {
return (InternalFileContent) FileContent.create(ifl);
}

@Override
public InternalFileContent getContentForInclusion(String path, IMacroDictionary macroDictionary) {
return (InternalFileContent) FileContent.createForExternalFileLocation(path);
}
};

IIncludeFileResolutionHeuristics ifrh = new IIncludeFileResolutionHeuristics() {

String[] classPath;

{
classPath = new String[1];
classPath[0] = "/usr/include";
}

@Override
public String findInclusion(String include, String currentFile) {
if (include.contains("/")) {
include = include.substring(include.lastIndexOf("/") + 1, include.length());
}

FileSearch fileSearch = new FileSearch();

for (int i = 0; i < classPath.length; i++) {
fileSearch.searchDirectory(new File(classPath[i]), include);
}

if (fileSearch.getResult().size() == 0)
return null;

String[] temp = new String[fileSearch.getResult().size()];

for (int i = 0; i < temp.length; i++) {
temp[i] = fileSearch.getResult().get(i);
}

String bestLocation = selectBest(temp, currentFile.toCharArray());
if (bestLocation == null)
return null;
return bestLocation;
}

private String selectBest(String[] files, char[] currentFullPath) {
String best = files[0];
int bestScore = computeScore(best.toCharArray(), currentFullPath);

for (int i = 1; i < files.length; i++) {
String file = files[i];
int score = computeScore(file.toCharArray(), currentFullPath);
if (score > bestScore) {
bestScore = score;
best = file;
}
}
return best;
}

private int computeScore(char[] path1, char[] path2) {
final int limit = Math.min(path1.length, path2.length);
int match = 0;
for (int i = 0; i < limit; i++) {
if (path1[i] != path2[i])
break;
if (path1[i] == '/')
match = i;
}
// Prefer shortest path with longest matches with.
return (match << 16) - path1.length;
}
};

crf.setIncludeResolutionHeuristics(ifrh);

ast = lang.getASTTranslationUnit(fc, new ScannerInfo(), crf, null, 0, ParserUtil.getParserLogService());
//I'm not sure how to compile the files


} catch (CoreException e) {
e.printStackTrace();
}
}

public static class FileSearch {

private String fileNameToSearch;
private List<String> result = new ArrayList<String>();

public String getFileNameToSearch() {
return fileNameToSearch;
}

public void setFileNameToSearch(String fileNameToSearch) {
this.fileNameToSearch = fileNameToSearch;
}

public List<String> getResult() {
return result;
}

public void searchDirectory(File directory, String fileNameToSearch) {

setFileNameToSearch(fileNameToSearch);

if (directory.isDirectory()) {
search(directory);
}

}

private void search(File file) {

if (file.isDirectory()) {
// System.out.println("Searching directory ... " +
// file.getAbsoluteFile());

// do you have permission to read this directory?
if (file.canRead()) {
for (File temp : file.listFiles()) {
if (temp.isDirectory()) {
search(temp);
} else {
if (getFileNameToSearch().equals(temp.getName())) {
result.add(temp.getAbsoluteFile().toString());
}

}
}

}
}

}

}
}
Re: Newbi Question regarding complete C enviorment [message #1707504 is a reply to message #1707207] Sun, 06 September 2015 07:37 Go to previous messageGo to next message
Tom Cat is currently offline Tom CatFriend
Messages: 5
Registered: September 2015
Junior Member
Thanks , Do you have en example for you first suggestion ?
Re: Newbi Question regarding complete C enviorment [message #1708130 is a reply to message #1707207] Mon, 14 September 2015 06:53 Go to previous messageGo to next message
Fadi J is currently offline Fadi JFriend
Messages: 11
Registered: June 2013
Junior Member
You have to do something like this. but c project instead of java project. Also info about headless eclipse plugins you can find with a google search.

public class ConsoleApp implements IApplication {

@Override
public Object start(IApplicationContext context) {
try {
String[] args = (String[]) context.getArguments().get(
"application.args");

Platform.getBundle("AnyBundleYouNeedsToStart").start();


} catch (Exception e) {
e.printStackTrace();
}

importSourceCode("path to source");

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Temp Project");
try {
project.delete(true, null);
} catch (CoreException e1) {
}
return IApplication.EXIT_OK;
}

@Override
public void stop() {

}

private static String importSourceCode(String absolutePathToDocument) {
IProject project = null;
String srcFolderLocationInTheProject = "src";
String projectName = "Temp Project";
try {
// Create a project
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(projectName);
if (project.exists()){
project.delete(true, null);
}
project.create(null);
project.open(null);

// Because we need a java project, we have to set the Java nature to
// the created project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });
project.setDescription(description, null);

// create Java project
IJavaProject javaProject = JavaCore.create(project);

// Set the Java build path
IFolder binFolder = project.getFolder("bin");
binFolder.create(false, true, null);
javaProject.setOutputLocation(binFolder.getFullPath(), null);

// Link source folder
ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(
Arrays.asList(javaProject.getRawClasspath()));
// remove all other source entries, because the main project folder
// is a source entry and we can't save other source entry nested in
// it
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getEntryKind() == IClasspathEntry.CPE_SOURCE) {
entries.remove(i);
}
}
IPath srcPath = javaProject.getPath().append(
srcFolderLocationInTheProject);
IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);
entries.add(srcEntry);
javaProject.setRawClasspath((IClasspathEntry[]) entries
.toArray(new IClasspathEntry[entries.size()]), null);

//link the folder with the resources
IFolder srcFolder = project.getFolder(srcFolderLocationInTheProject);
srcFolder.createLink(new Path(absolutePathToDocument.replaceFirst("file:/", "")), IFolder.FOLDER, null);

return "file:/"
+ ResourcesPlugin.getWorkspace().getRoot().getLocation()
+ "/" + projectName + "/" + srcFolderLocationInTheProject;
} catch (Exception e) {
e.printStackTrace();
try {
project.delete(true, null);
} catch (CoreException e1) {
e1.printStackTrace();
}
return null;
}
}
}
Re: Newbi Question regarding complete C enviorment [message #1708210 is a reply to message #1708130] Mon, 14 September 2015 16:15 Go to previous messageGo to next message
David VavraFriend
Messages: 1417
Registered: October 2012
Senior Member
You could use File==>Import==>C/C++==><whatever>

You will need to decide


    If you want to retain the batch files as the build method or
    if you want Eclipse to use a make file and where the make file is to come from.
    Where the project and source will reside: original location or in the workspace


I personally don't like keeping things in the workspace. Some of the dislike comes from earlier versions of Eclipse that slowly mangled the workspace. Things are better now but the prejudice remains. To keep the original location, change the setting in the import menu.

If you want to retain using the batch files as you will need to change Build Command in Project properties ==> C/C++ Build

Each option has ramifications and it can get quite involved listing them. So I suggest you read:
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Fgetting_started%2Fcdt_w_import.htm
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Fgetting_started%2Fcdt_w_existing_code.htm

and come back here with more specific questions. The first link is a tutorial but you should read both before proceeding further.



[Updated on: Mon, 14 September 2015 16:17]

Report message to a moderator

Re: Newbi Question regarding complete C enviorment [message #1752173 is a reply to message #1708210] Thu, 19 January 2017 13:09 Go to previous message
Jurgen Vinju is currently offline Jurgen VinjuFriend
Messages: 5
Registered: July 2009
Junior Member
Hi Fadi J,

We love your Test snippet here at the Rascal project (http://www.rascal-mpl.org), but we are now wondering what the actual license of the snippet is. We plan to copy it, adapt it and re-organize it into our own library for analyzing C and C++ programs, but not without giving credit where it is due and not without checking if there are other copyright owners.

a) do you own the copyright of this code and are you willing to publish it as open-source?
b) are there any other copyright owners of this code that we should contact?
c) do you agree to publish this code under a BSD license?

Thanks!

Jurgen.Vinju@cwi.nl
Previous Topic:No desired console otput
Next Topic:Visual Studio toolchain in Eclipse for C++
Goto Forum:
  


Current Time: Sat Jan 29 05:31:52 GMT 2022

Powered by FUDForum. Page generated in 0.02976 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top