fasked
fasked

Reputation: 3645

Eclipse CDT shows semantic errors, but compilation is ok

I have installed Eclipse Indigo for C/C++ Linux developers on Ubuntu 10.04 x86.

When I use common predefined macro __BASE_FILE__ Eclipse says Symbol '__BASE_FILE__' could not be resolved, but compilation is OK. I have to use it so often in my code and Eclipse fills my screen with red lines and bug icons :)

How can I fix this?

Upvotes: 38

Views: 57129

Answers (20)

Donal Fellows
Donal Fellows

Reputation: 137777

In my case, I eventually discovered that the indexer was having problems because a colleague had “helpfully” duplicated a whole load of declarations in two header files that were only included together in a small subset of C files in the project. Removing the duplications (and making one header include the other) allowed the indexer to function correctly again.

It was particularly a problem with duplicated enumerations, for some reason. I do not know why this is so.

Upvotes: 1

George Eliozov
George Eliozov

Reputation: 71

This is a file indexing issue.

Solution - Right click on the project, Index->Freshen all files.

(Applies to Eclipse CDT.Oxygen)

Upvotes: 1

leonix
leonix

Reputation: 1

The problem occurs this way: I insert a new variable name into the code somewhere e.g. "newone" in this example:

int a;

foo()
{
  a=17;
  newone=23;
}

The file is saved (so the indexer is reindexing). Then I added the definition:

int a, newone;

foo1()
{
  newone=0;
}


foo()
{
  a=17;
  newone=23;
}

The indexer will still shows the error at the line " newone=23;" but not at the other lines of code containing "newone".

Solution: first define your variables, then use it.

M.

Upvotes: 0

Tanghuihua
Tanghuihua

Reputation: 61

Deleting the .metadata folder , and then Import project .

This way is OK!

Upvotes: 6

Zazun
Zazun

Reputation: 79

The simplest solution is to reset the indexer:

Window / Preferences / C/C++ / Indexer.
uncheck "Enable indexer" ->>OK
rebuild all, may show lots of errors
check "Enable indexer" ->>OK
rebuild all

This error can be produced by forced closing of eclipse by power failure.

Upvotes: 8

user245225
user245225

Reputation: 11

try this:

Preferences --> c/c++ --> Indexer --> Index all header variants

check this option.

Upvotes: 1

oscar
oscar

Reputation: 11

I have the same problem. Compiler preprocesses ok, but static analyzer doesn't. In my source file:

#define PLATFORM_INC_FILES
#include <platform.h>

int coflags=O_BINARY; // Undefined symbol

In platform.h (an OS dependent header to help creating portable code):

#ifdef PLATFORM_INC_FILES
    #include <stdio.h>
    ...

It looks like static analyzer doesn't take the .c #define statements into account when preprocessing included headers. By defining it at the symbols dialog, errors dissapear, but that is a project scope symbol definition, resulting in every source to include every system header in my case.

Hope it gets fixed soon...

Using Eclipse Mars and MinGW on MSIS2.

Upvotes: 0

mczraf
mczraf

Reputation: 171

You need to rebuild the index of your project.

Right-click on the project, then Index->Rebuild.

Upvotes: 7

Scott
Scott

Reputation: 1

I had a similar issue but my compile was fine but Eclipse showed errors on lines that used a #define from another include file. I fixed by checking "Index all header variants". I think the classis #ifndef FILENAME in include file was process multiple times in Indexer and so 2nd time the FILENAME was defined so all #defines in .h file were not "seen" by the preprocessor. Go to Window | Preferences | C/C++ | Indexer.

Upvotes: 0

Aaron Swan
Aaron Swan

Reputation: 1306

Not sure if this works for the OPs issue, but I had semantic errors in eclipse Luna and was able to resolve them by following instructions in this thread: Eclipse shows unresolved inclusion but it compiles

The compiler finds the header, but Eclipse not.

You could help Eclipse and set the path to the header files under:

Project -> Properties -> C/C++ Build -> Settings -> Compiler -> Includes

In my situation, eclipse had determined one of my include directories, but did not determine that there were sub-include directories within it.

Upvotes: 0

danielbker
danielbker

Reputation: 412

If on Ubuntu, go to Window > Preferences > In Search box type "Indexer" > Select Indexer in left column. Under heading "Indexer Options" tick the following options (all but those involving the skipping of files):

  • Index files source files not included in project
  • Index unused Headers Index all header variants
  • Index source and header files openend in editor
  • Allow heuristic resolution of includes

Under heading "Indexing Strategy" tick both options which are:

  • Automatically update the index
  • Update index immediately after every file-save

Under heading "Build Configuration for the indexer" select the following option:

  • User active build configuration

Upvotes: 2

some bits flipped
some bits flipped

Reputation: 3092

adding as another answer, hopefully this will help someone.

I have a ~simple workspace (1 c++ shared-object (linux's version of a DLL, 3 c++ executables, and a pydev python project)

I checked out all the code to a new machine, and it builds fine but had numerous 'semantic errors' on std-c and std-C++ code and includes.. I went through all the discovery / indexer setting to no avail.

==> deleting the entire .metadata folder fixed this.

Since i had nothing non-trivial set up in the workspace (all in the project files / makefiles), eclipse happily created a fresh workspace, and i simply had to do a file->import-> existing projects

Upvotes: 1

Denis Shchepetov
Denis Shchepetov

Reputation: 539

I used #pragma once in my code. This configuration caused me to have the problem:

---fileA.h---

#pragma once
#define MYMACRO(X) func(X)

---fileB.h---

#include "fileA.h"

---fileB.cpp---

#include "fileB.h"

MYMACRO(5)  <---- warning here

I replaced #pragma once with #ifndef #define HEADER_FILE_H #endif, and that fixed the problem.

Upvotes: 0

sidney_h
sidney_h

Reputation: 49

I think it has something to do with the workspace/.metadata. I had the problem of semantic errors reported but Hello World compiles and runs. I deleted the project, created another one, same error reporting. Reinstalled CDT, same thing. Deleted the workspace, shut down Eclipse, restarted, created new hello world, same thing. Deleted the workspace again, shut down Eclipse again, this time noticed that Eclipse had recreated the workspace folder during shutdown when it couldn't find it. Deleted the workspace with Eclipse shut down. Restarted Eclipse, created HW project, errors show for about a second and then gone. Deleted project, created another one, errors show for a sec, then poof. If you actually had projects in your workspace (mine was a clean install), I bet that you could just delete the .metadata folder within the workspace, and it would fix it.

Upvotes: 3

WindRider
WindRider

Reputation: 12318

Window -> Preferences -> C/C++ -> Code Analysis -> Syntax and Semantic Errors: OFF

This won't solve the cause of the problem but at least will stop the false errors from being shown.

Upvotes: 24

rjklindsay
rjklindsay

Reputation: 1

In the past, I would define the ANDROID symbol in: Project -> Properties->C/C++ General -> Paths and Symbols -> #Symbols tab However, the latest version of Eclipse no longer has a # Symbols there, or anywhere else. It seems there is no longer any way of defining symbols in Eclipse. Eclipse has so many bugs and problems. I finally gave up and switched to using Gedit and the terminal to compile.

Upvotes: -1

fluke
fluke

Reputation: 680

I personally agree with compostus' answer.

It's good to solve the real problem that eclipse's code analyzer have.

But when I try this steps: define the symbol in Project -> Properties->C/C++ General -> Paths and Symbols

I don't find symbol tab or any place to add a symbol. what I want to do is putting macros like -DANDROID or #define ANDROID 1 to eclipse so that the code analyzer can find this definition.

I'm under Mac OSX mountain lion.

Upvotes: 1

compostus
compostus

Reputation: 1228

IMO the correct solution is to define the symbol in Project -> Properties->C/C++ General -> Paths and Symbols -> Symbols tab. Since you can assume the symbol will always be supplied by the compiler, you just tell Eclipse to consider it defined.

I had the same problem just recently, where Eclipse complained about undefined macro, that i always supply using -D<MACRO_NAME> compiler option.

Upvotes: 3

taloft
taloft

Reputation: 261

Not sure if this addresses your specific problem, but I also had semantic errors. They just came out of the blue after having a working project.

Anyway, I fixed it with a single option in workspace settings by setting: "Build configuration for indexer: Use active build configuration"

The other option was "Use the build configuration specified in the project's indexer settings" and this is the one that wasn't working. I think it may have been corrupted after an eclipse crash.

Upvotes: 26

ondrisko
ondrisko

Reputation: 320

Updated: You have to add proper paths to Project->Propoerties->C/C++ General->Paths and Symbols. If it does not help, you can configure Eclipse Code Analyser (which generates the 'errors') in Project->Properties->C/C++ General->Code Analysis. Under the error description you have problem with, try Customize Selected->Scope->Exclusion Patterns.

Upvotes: 3

Related Questions