Sunspots

The Sun, eclipses, auroras and space weather explained

Eclipses

Fix Build Path Errors in Eclipse for Embedded C/C++

· 6 min read · Eclipses

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Build path errors in Eclipse for embedded C/C++ projects usually mean the compiler cannot find headers, libraries, or source files, or the project settings point to wrong locations. Fixing them involves checking the project properties, adjusting paths and symbols, and cleaning the build. This guide walks you through the most common causes and solutions.

What You'll Need

  • An Eclipse IDE with CDT (C/C++ Development Tooling) installed (most Eclipse packages for embedded developers include it).
  • Your embedded toolchain (e.g., GCC ARM Embedded, AVR-GCC) installed and its path known.
  • Your project source code and any third-party libraries (like CMSIS, HAL drivers, or RTOS) available on your computer.

Step-by-Step

  1. Open the Problems View. Go to Window > Show View > Problems. This lists all build errors. Look for errors like "fatal error: xxx.h: No such file or directory" or "undefined reference to ...". They tell you exactly what's missing.
  2. Right-click your project in the Project Explorer and select Properties (or press Alt+Enter with the project selected).
  3. Check the C/C++ Build > Settings > Tool Settings tab. Under Cross GCC Compiler > Includes (or similar, depending on your toolchain), you'll see a list of include paths. Add the directories that contain your header files. Use the Add button and browse to the folder. For example, if you have a inc/ folder in your project, add ${workspace_loc:/YourProjectName/inc} or just the absolute path.
  4. Verify the compiler and linker paths. Under the same Tool Settings, check Cross GCC Compiler > Command and Cross GCC Linker &em; Command. Make sure they point to your actual toolchain executables (e.g., arm-none-eabi-gcc and arm-none-eabi-g++). If not, set the correct command or adjust the Cross Settings to point to the toolchain bin folder.
  5. Add library paths and libraries. If you get "undefined reference" errors, you likely need to link libraries. In Tool Settings, go to Cross GCC Linker > Libraries. Add library search path (-L) and library names (-l). For example, for libm.a, add m in the libraries list, and add its folder in the library path.
  6. Check the C/C++ General > Paths and Symbols tab. This is an alternative way to add includes and defines. Click the Includes tab, select your language (C or C++), and add the same include folders. Also, under Symbols, add any preprocessor defines (e.g., STM32F407xx) that your code expects.
  7. Set the source location. In the same Paths and Symbols dialog, go to the Source Location tab. Ensure your source folders (like src/) are listed. If you moved your code, add the new folder and remove the old one.
  8. Clean and rebuild. After making changes, go to Project > Clean..., select your project, and then Build. This forces a full recompile so stale object files don't mask errors.
  9. Check the build output. Open the Console view (Window > Show View > Console) and look at the actual compiler commands. They show the exact include paths and flags used. If a path is missing, you'll see it there.

Common Mistakes

  • Using relative paths incorrectly. Eclipse often resolves relative paths from the project root, but if your project is in a different location or you have linked resources, paths like ../lib may break. Prefer absolute paths or use Eclipse variables like ${workspace_loc}.
  • Forgetting to add the compiler's own include paths. Some toolchains require internal headers (like newlib or libgcc). These are usually added automatically, but if you changed the toolchain path, they may be lost. Check the Includes list for paths like /opt/gcc-arm-none-eabi/arm-none-eabi/include.
  • Adding library names with the lib prefix or .a extension. In Eclipse, you enter just the base name (e.g., m for libm.a). Adding lib or .a causes linker errors.
  • Not adding the linker search path. Even if you add the library name, the linker must know where to find it. Always add the folder containing the library under Library Path.
  • Editing files outside Eclipse. If you modify source files or headers with an external editor, Eclipse may not detect changes. Refresh the project (F5) before building.

Tips

  • Use Eclipse variables like ${workspace_loc:/MyProject/inc} to make paths portable across machines.
  • If you have multiple projects, consider using linked resources to share common headers (e.g., a shared HAL library). Right-click project > New > Folder > Advanced > Link to alternate location.
  • Keep your toolchain path in the Window > Preferences > C/C++ > Build > Environment if you use environment variables like PATH. This helps Eclipse find the compiler.
  • Use the Indexer to check for unresolved includes before building. Right-click project > Index > Rebuild. If the index shows errors, the build likely will too.
  • For STM32 or other MCU projects, consider using the STM32CubeIDE or similar vendor IDE if you're stuck, as they preconfigure paths for the HAL.

Troubleshooting

If the error persists after adding includes:

  • Check the Console to see the actual compiler command. Sometimes Eclipse adds a -I flag that overrides your settings, or the path is being quoted incorrectly.
  • Make sure the header file actually exists in the path you added. Double-click the error in the Problems view; Eclipse will try to open the missing file, and you can see the expected path.
  • If you're using a Makefile-based build, the build path is defined in the Makefile, not in Eclipse settings. In that case, edit the Makefile or switch to a managed build (Project Properties > C/C++ Build > Builder Settings > uncheck "Generate Makefiles automatically" and specify your own).

If you get "undefined reference" even after adding libraries:

  • Check the order of libraries. The linker processes libraries left to right; if library A depends on B, B must come after A. Reorder the library list.
  • Ensure the library is compiled for the same architecture (e.g., ARM vs. x86). A mismatch causes undefined references.

If the project builds but the index still shows errors:

  • Right-click the project > Index > Rebuild. The indexer may be out of sync.
  • Check Project Properties > C/C++ General > Indexer and enable "Index all files" if it's not.

Frequently Asked Questions

Why does Eclipse say "Program "gcc" not found in PATH"?

This means Eclipse can't find your compiler. Go to Window > Preferences > C/C++ > Build > Environment and add the toolchain's bin directory to the PATH variable, or set the compiler command in project properties to the full path.

How do I add a preprocessor define like "STM32F4"?

In Project Properties > C/C++ General > Paths and Symbols > Symbols, select the language (C or C++), click Add, and enter the name (e.g., STM32F4). You can also add it in the compiler command line under C/C++ Build > Settings > Tool Settings > Cross GCC Compiler > Miscellaneous as -DSTM32F4.

What does "cannot find -lfoo" mean?

The linker can't find the library file libfoo.a or libfoo.so. Add the directory containing that file to the Library Path in the linker settings, and ensure the library name is entered as foo (without lib or extension).

Why does my project build fine on another computer but not here?

Paths are likely absolute and machine-specific. Replace them with Eclipse variables like ${workspace_loc} or use relative paths. Also check that the toolchain is installed in the same location or update the path in project properties.

How do I fix "fatal error: no such file or directory" for a header in a subfolder?

Add the subfolder to the include paths. For example, if you have src/utils/helper.h, add src/utils to the include list. Alternatively, include it as #include "utils/helper.h" and add src to the include path.

Should I use "Paths and Symbols" or "Tool Settings" to add includes?

Both work, but they affect different aspects. Paths and Symbols is used by the indexer for code completion and error highlighting, while Tool Settings affects the actual compiler command. For consistency, add includes in both places, or use Tool Settings and then rebuild the index.