0

Hello how to rewrite probably bad construction?
I tryed to ask how to fix it to make it work there,but maybe it's all the bad conception. Any other solution to do that? It's compiled in Eclipse using GCC for linux, compiled as C code.

file first.h

#ifndef FIRST_H_
#define FIRST_H_

typedef struct foo
{
    int a;
    char *c;
} foo_struct;

#endif /* FIRST_H_ */

file second.h:

#ifndef SECOND_H_
#define SECOND_H_

#include "first.h"

typedef struct wtf
        {
        foo_struct *poleFOO[5];
        }wtf_struct;

#endif /* SECOND_H_ */

Concretely in file second.h row foo_struct *poleFOO[5]; throws: "foo_struct could not be resolved" I work on Linux Ubuntu 11.10 using gcc in editor Eclipse for C and C++.

Community
  • 1
  • 1
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • There isn't anything obviously wrong here. Please post a single-file, self-contained example that reproduces the problem. Also, please be clear whether you are compiling as C or C++. – zwol Jan 26 '12 at 17:23
  • @Zack it' in C I updated it. What file? there are two files first.h, second.h, problem is in second.h when I use type foo_struct defined in first.h – user1097772 Jan 26 '12 at 17:32
  • 1
    `#include "first.h"` operates as-if the full text of `first.h` were inserted verbatim in place of the `#include` line. I wanted you to manually make that transformation and see if the problem goes away. – zwol Jan 26 '12 at 17:46
  • However, having tried it myself and seen GCC have no problem with the files as presented, and also based on the phrasing of the error, I think this is an Eclipse problem rather than a C or GCC problem. Have you tried Emacs? ;-) – zwol Jan 26 '12 at 17:48
  • ... That was mostly a joke. Emacs is excellent but has a very steep learning curve and a completely different key-command structure from what you are probably used to; I don't actually recommend trying to switch unless you can spare a month to retrain your fingers. It does not have a built-in equivalent of Eclipse's intellisense but there are add-ons that provide it; I hear good things about [Semantic](http://cedet.sourceforge.net/semantic.shtml). – zwol Jan 26 '12 at 17:55
  • I *do*, however, recommend giving up on Eclipse; I don't know *anyone* who has used it successfully for C or C++. You might have a look at [Sublime Text](http://www.sublimetext.com/2). – zwol Jan 26 '12 at 17:58
  • Don't you have a `*.c` file that includes one or both of your `.h` files? Can you show us that? – Keith Thompson Jan 26 '12 at 19:47
  • @Zack if I put it in one file = instead of #include "first.h" I just copy whole code of first.h it works fine .. but separately (= in two files like there with include) not .. – user1097772 Jan 26 '12 at 20:35
  • @KeithThompson I don't have it yet completely done. But there is problem that I want include the first.h to second.h to be able to use the structure from first header in second header and already there occurs the error .. – user1097772 Jan 26 '12 at 20:39

5 Answers5

3

Okay, this is not an error from the compiler but from Eclipse. Simply Googl'ing the error "could not be resolved" points me to articles talking about Eclipse CDT (the eclipse subsystem for C/C++ development).

So it has something to do with Eclipse, your C headers look syntactically right. I believe that without a C file but only headers, Eclipse does not know how to parse the headers only to create its own index database (must be used for intellisense, symbols list, etc.)

I suggest you insert a simple C file including second.h, and with a main() function so that the link step passes as well, for example:

#include "second.h"

int main() {
    wtf_struct my_variable;
    return 0;
}
chickenkiller
  • 426
  • 2
  • 5
  • OMFG! Dame Eclipse :/ I spent day solving this dame problem :( Thank you very much .. all works well now :) For the record there is also needed add #include "first.h" and it suprisingly works :) – user1097772 Jan 26 '12 at 21:14
1

I had the same Error: "vuint16 - could not be resolved" for a wrapped type within typedef (mentioned below) and it was successfully resolved by rebuilding the Index in Eclipse Environment(right click on project -> Index -> Rebuild) Note: It is not a compiler error!

[headerfile1: can.h]
#define vuint8 uint8

[headerfile2: can_local.h]
#include "Can.h" /* include all needed types */
typedef struct sCanRxFullInfoStruct
{
  vuint16 objectNumber; //error line
} tCanRxFullInfoStruct;
0

I had the same problem after creating a set of typedefs that wrap other types. The solution was to rebuild Eclipse Index - right click on project -> Index -> Rebuild

0

I landed here because I had a similar problem and Eclipse was also to blame (I reproduced using only GCC and it works). I tried to reproduce the OP's issue, but it compiled OK in my environment :/. So I will show what worked for me instead.

The only thing that helped was to make the extern declaration using struct, and the actual definition in the source file using the new type I defined with typedef, like so:

lib1.h:

typedef struct sMyStruct
{
    // struct fill
}tMyStruct;

lib2.h:

extern struct sMyStruct my_struct, *p_my_struct;
//extern tMyStruct my_struct, *p_my_struct; // This wouldn't work

lib2.c:

tMyStruct my_struct, *p_my_struct;

With extern tMyStruct in lib2.h it would not compile (the typedef was not recognized), no matter what. Cleaning the project wouldn't help, neither would rebuilding the index, etc. Astonishingly, I tried to reproduce the problem minimally in a new project... and it compiles fine! With both versions! So, I don't know, Eclipse got hung up on some wicked ghost file or something. I was stuck for days, the only thing that worked on the actual project was what I showed above.

0

I suspect that you've created (and included) two headers that both have the same header guard

#ifndef FIRST_H_
#define FIRST_H_
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Where do you see that? I created for testing this new C project. There are only two files that you can see there first.h, second.h header guard in fist file is FIRST_H_ and in second is SECOND_H_ It's seems different to me .. Anyway if i comment that tree lines with ifndef, define, endif in both files cause for testing it's not necesary, the problem is same .. so I guess it's not the problem ... – user1097772 Jan 26 '12 at 17:36
  • @user1097772: Are you saying that there are no source files in this project? Only two header files? – Drew Dormann Jan 26 '12 at 17:40
  • At the moment yes, I have also source file where I wanna use it but this problem occurs when I try to include one header file in another file. Maybe I have programing habbits which are not good for C. I'm used to work in java. But I'm forced to do the server in C. You can see from the link at the start of my question what I originaly wanted. I do game server - n games, each games has two players and I wanna store the data abou the game states, player info ... on the server. So struct game, in game are players => struct player and one variable in the game will be array of players .. – user1097772 Jan 26 '12 at 20:31
  • @user1097772: As Zack said right away, the code you posted in your question is fine. The problem is in something you're not describing. – Drew Dormann Jan 26 '12 at 22:29
  • Yes problem was in Eclipse editor. It didn't understand it until I've included the header aslo in some source code. Then Eclipse waked up an started understand it correctly. I just din't expected behaviour like that. But all works fine now and I know I shouldn't rely that editors wors always fine .. – user1097772 Jan 27 '12 at 09:07