Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse CDT C/C++: Include a header file from another project

Tags:

I have two c++ projects in Eclipse CDT main and shared. In shared i have a header called calc.h. I want to use this header in main, so i did the following:

  • added #include "calc.h to the relevant files in main
  • In main's properties -> Project references i checked of shared

I hoped this would work, but I get a fatal error: calc.h: No such file or directory when compiling, so the project reference somehow doesn't work.

I can get it to work by manually adding shared's source folder in main's properties->C/C++ Build->Setting->GCC C++Compiler->Includes, but my i have a bad feeling that this will become cumbersome on larger projects more complex dependencies. I'll therefore hoped that Eclipse could handle this via project references.

Am I missing something or is manually the only way?

like image 319
Tobber Avatar asked Feb 14 '12 06:02

Tobber


People also ask

HOW include header file in C Eclipse?

Select C/C++ General -> Path and Symbols. Select Includes tab. In Languages list, select 'GNU C' or whatever C compiler tool chain you use. Press 'Add...' button and add the directory for the include files.

Can you include header files in header files C?

In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension.

How do I include another header file in C++?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.


1 Answers

You are right, that is the way to do it!

I use Eclipse CDT on large projects, but I don't use the Eclipse compiler settings. There are some drawbacks to using the CDT compiler's settings:

  • As you said, on large projects, it is cumbersome.
  • If you want to compile your project on a platform which doesn't have Eclipse (when you deploy your application), it is not straightforward.

I use CMake to manage my Eclipse projects. When I start a new project, I do the following steps:

  1. In a terminal, create a folder for your new project.
  2. With your favorite text editor (vim, emacs, Text edit, kate, etc...) create the CMakeLists.txt file for your project. You don't have to create an exhaustive CMakeLists, just a small CMakeLists for your first files is enough.
  3. Then, ask cmake to generate the Eclipse project like this:
    cmake -G "Eclipse CDT41. Unix Makefiles"
    
  4. Open Eclipse, click on File --> Import, and choose "General/Existing project into workspace". Choose the folder created in the first step, and your project is ready to use in eclipse.

CMake is THE compiler configuration tool to manage projects... If you don't know this I encourage you to discover it.

Cheers!

like image 86
Adrien BARRAL Avatar answered Sep 17 '22 22:09

Adrien BARRAL