0

In my project, a structre is being used in several functions.

like this:

void function1 (Struct_type1 * pstType1);

but when I search for Struct_type1 's references, I can't find any. This Structure must be defined somewhere. How to find the definition?

OS- Windows

Edit: I think its difficult to answer this without source code and I can't share that big project here. So, I've changed my question to:

  • Is Hidden Declaration possible in an embedded project?

(by hidden I mean no one can see the definition.)

SKD
  • 464
  • 1
  • 4
  • 16

2 Answers2

5

Is Hidden Declaration possible in an embedded project?

If you have access to all source code in the project, then no.

This is only possible in one specific case, and that is when you have an external library for which you don't have the C code, you only have a header file and an object file or lib file (or DLL etc).

For such cases it is possible (and good practice) for the library header to forward-declare an incomplete type in the header, and hide the actual implementation in the C file which you don't have access to.

You would then have something like this in the h file:

typedef struct Struct_type1 Struct_type1;

The compiler might often do things like this with its own libraries too, if they want to hide away the implementation. One such example is the FILE struct.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I have added the whole `Tree` of my project to Source Insight. There is no file other that that tree. Then is it possible that some of files in the project are not accessible by source-insight? – SKD Feb 22 '16 at 12:30
  • And as far as .`o` files are concerned, they are erased while `purge` – SKD Feb 22 '16 at 12:31
  • @SKD I have no idea what Source Insight is capable of, but any half-decent IDE out there should be able to find a declaration/definition. Usually there's an menu option to find it if you right click on the type name. I can't think of any IDE on the market which doesn't have this feature. Otherwise, you could always download Codeblocks and dump your whole project there, just for the search feature. – Lundin Feb 22 '16 at 12:38
  • I can't use any other IDE . Company policy :( – SKD Feb 22 '16 at 12:49
  • 1
    @SKD I mean just for sanity checking the project, not for developing. Also, doesn't your IDE have an option to text search all files in the project? You can always resort to that. – Lundin Feb 22 '16 at 12:52
  • Yes it has an option to lookup in all files. but it is not showing the definition one – SKD Feb 22 '16 at 12:54
  • @SKD Then there's just two options left: either the struct is something forward-declared by the compiler and hidden from you, or the project doesn't compile. Be aware that there might be various libs linked to your project through misc compiler settings. – Lundin Feb 22 '16 at 12:57
  • What does *forward-declared* and *hidden from you* means? – SKD Feb 22 '16 at 13:02
  • .. or those functions that reference 'Struct_type1' are not in the build. – Martin James Feb 22 '16 at 13:02
  • 2
    @SKD Forward declaration == declaring an object of incomplete type. It means that they have implemented their library as an opaque type. [Here is an example](http://stackoverflow.com/questions/13032015/how-to-implement-a-class-in-c/13032531#13032531). – Lundin Feb 22 '16 at 13:49
5

Not an answer, but possibly a way to find the answer. Idea: Let compiler help you.

Define the struct yourself, then look at compiler errors like "struct struct_type1 is already defined in... at line ..."

If you get no compiler error in this case, maybe the struct is only forward declared, but not defined.

To explain why this is sometimes done, here a bit of code:

// Something.h

struct struct_type1; // Forward declaration.
struct struct_type1 *SomethingInit();
void SomethingDo( struct struct_type1 * context );

In code looking like the above, the definition of the struct is hidden inside the implementation. On the outside, it need not be known, how the struct is defined or its size etc, as it is only traded as a pointer to the struct (and never as a value). This technique is used to keep internal types out of public header files and used often by library designers. You can think of it as an opaque handle of sorts.

But then, you still should be able to find the forward declaration, albeit not the definition.

BitTickler
  • 10,905
  • 5
  • 32
  • 53
  • Heh! Good plan:) I knew error-messages were good for something. – Martin James Feb 22 '16 at 12:42
  • 1
    Doesn't work if the struct is incomplete type. At best you'll get a linker error, which is probably not as specific as a compiler error would have been. – Lundin Feb 22 '16 at 12:51
  • 1
    OK, then, what happens if you deliberately corrupt your original declaration? Change to 'void function1 (Struct_type1z * pstType1);'. Does the compiler generate any hints with the expected error? – Martin James Feb 22 '16 at 12:53
  • @SKD augmented answer a bit. (reload..) – BitTickler Feb 22 '16 at 12:57
  • Thanks for your efforts, but even if it is declared like that, then I must be able to find that declaration. – SKD Feb 22 '16 at 13:07