Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 356425

Summary: Eclipse cannot understand unistd.h and getopt.h together when importing a project
Product: [Tools] CDT Reporter: patrick
Component: cdt-otherAssignee: Doug Schaefer <cdtdoug>
Status: RESOLVED WORKSFORME QA Contact:
Severity: major    
Priority: P3 CC: 279trex, eclipse, johnbmcd, yevshif
Version: 8.0   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description patrick CLA 2011-08-31 23:34:40 EDT
Build Identifier: 20110615-0604

If you include both unistd.h and then getopt.h (in that order) and then import the project, Eclipse will claim that getopt_long() is undefined.  The only solution is to change the include order, delete the project, and re-import it.

Reproducible: Always

Steps to Reproduce:
1. Create a folder in your workspace and a file in the folder called main.cpp.
2. In a different text editor, enter the following into main.cpp:

#include <unistd.h>
#include <getopt.h>

static struct option long_options[] = {
   {0, 0, 0, 0}
};

int main(int argc, char *argv[])
{
    int option_index = 0;
    int c = getopt_long(argc, argv, "", long_options, &option_index);
    return 0;
}

3. Go to File > Import > Existing Code as Makefile Project
4. Type the location of the folder created in step 1
5. Select the Linux GCC toolchain
6. Open main.cpp
7. Observe that usage of getopt_long() is considered erroneous despite that the source builds without issue.
8. Swap the order of the includes in main.cpp and observe error does not disappear
9. Delete the project and repeat steps 3-6.  Observe that Eclipse now understands getopt_long()
10. Swap order of includes again.
11. Observe that Eclipse no longer complains, even with original include order.
Comment 1 Markus Schorn CLA 2011-09-06 08:52:33 EDT
I cannot reproduce the issue, it depends on the system includes. Most likely it goes away if you add getopt.h to the list of files to index up front. You can do so in the indexer preferences or on project level.
Comment 2 Markus Schorn CLA 2011-10-06 03:47:27 EDT
No response from submitter.
Comment 3 Alexander Hofbauer CLA 2011-10-29 20:10:20 EDT
(In reply to comment #1)
> goes away if you add getopt.h to the list of files to index up front

I have a similar problem here (on Linux with gcc 4.6), include order doesn't matter.

Adding getopt.h to the indexer as suggested solved it for me.
Comment 4 279trex CLA 2011-11-25 19:42:43 EST
I too have a similar problem on Ubuntu with gcc 4.5.2. Adding getopt.h to the indexer solved the problem for me.
Comment 5 John McDonald CLA 2014-02-13 11:54:58 EST
I am running Ubuntu 12.04 with g++ 4.6.3 and Eclipse CDT Indigo. I had the same problem -- which was also fixed by adding getopt.h to the indexed up front list.