How to configure a C/C++ project with Eclipse and CMake

本文介绍如何使用CMake配置Eclipse项目以实现跨平台编译。提供了推荐方法和手动方法两种方案,涵盖项目创建、MakeTarget设置及构建流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

author:Javier V. Gomez


How to configure a C/C++ project with Eclipse and CMake

I'm not an expert in CMake. Actually, I always programmed in Geany which is the most simple IDE and I run cmake . and make directly from the terminal or modifying a little bit the Geany configuration. Now, I'm starting to work with Eclipse (since is one of the most used environments for many many different programming languages). It was not easy to create a project which works with CMake (I want my programs to be as cross-platform as possible, so this was a requirement). The main reference site for this is the CMake WikiSite.

I will explain here two types: the recommended method and the manual method.

Contents

 [hide

Changing the user name in Eclipse

I know this is not the correct place for this one, but it is just a small trick. The variable ${user} in Eclipse takes the OS name, and this one is probably not the best for our templates. Just go to eclipse.ini (in the root folder of Eclipse) and add the following line: -Duser.name=<Insert name>

A brief example of my eclipse.ini (the necessary part, the .ini is larger):

openFile
-vmargs
-Dosgi.requiredJavaVersion=1.6
-XX:MaxPermSize=256m
-Xms40m
-Xmx384m
-Duser.name=Javier V. Gómez

Using CMake Eclipse CDT4

Starting with version 2.6.0 CMake includes a generator for Eclipse CDT 4.0 or newer. It works together with the Makefile generators (i.e. "Unix Makefiles", "MinGW Makefiles", "MSYS Makefiles", and maybe "NMake Makefiles"). This generator creates a set of .project/.cproject files that can be imported in Eclipse as an "Existing Eclipse project".

Note that CMake 2.4.x users may follow instructions provided here in order to setup an Eclipse+CMake usage manually.

Using the Eclipse CDT4 generator isn't different than using any other CMake generator. It works for in-source and out-of-source builds. In this example, we'll assume that the source tree of the project is /home/eric/certi_src.

Be sure to have a proper CMakeLists.txt file in the src directory. For instance, if you get an error such as Undefined Reference when you import into Eclipse, make sure you have the TARGET_LINK_LIBRARIES set correctly.

 TARGET_LINK_LIBRARIES(AwesomeProjectMain ITKCommon ITKIO ITKBasicFilters)

On Linux, these libraries may exist in the bin subdirectory under the ITK Root Directory with a ".a" extension.

Create a build directory, go there and run CMake (see below for commandline). Make sure you set your CMAKE_BUILD_TYPE to Debug if you want to debug your project with gdb inside of Eclipse CDT. This is not done automatically (especially when using cmake-gui)

 mkdir /home/eric/certi_build 
 cd /home/eric/certi_build
 cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug ../certi_src 

(IMPORTANT) Your project name should be different from your executable name and different from your build folder name. Otherwise, Eclipse will NOT pick up your executable as you build them. Since my build folder name is certi_build, a CMakeLists.txt file like below should work (notice the difference in project name and executable name)

 PROJECT(AwesomeProject)
 ADD_EXECUTABLE(AwesomeProjectMain
   main.cpp
   util.h
   util.cpp
 )

You will now find two Eclipse files in your build tree:

 certi_build/.project
 certi_build/.cproject

Import the created project file into Eclipse

  1. Import project using Menu File->Import
  2. Select General->Existing projects into workspace:
  3. Browse where your build tree is and select the root build tree directory. Keep "Copy projects into workspace" unchecked.
  4. You get a fully functional eclipse project

You can edit your CMakeLists.txt file inside of Eclipse CDT, a plugin called CMakeEd can help you with this task. When you edit your CMakeLists.txt file, you are recommended to delete your project and reimport it.

In-Source Builds

In-Source builds are fully supported by the Eclipse project generator.

Out-Of-Source Builds

Eclipse has two issues with out-of-source builds, the project generator tries to work around them as best as it can. The details are described below.

Version Control Integration in Eclipse

Eclipse supports version control systems, e.g. cvs and svn, but for them to work, the project files must be at the root of the source tree. This is not the case with out-of-source builds. The only way to get version control for your project in Eclipse is to have a separate project in the source tree for this purpose. You can either create this project manually or tell CMake to create it for you when creating your project files:

cmake -G"Eclipse CDT4 - Unix Makefiles" -DECLIPSE_CDT4_GENERATE_SOURCE_PROJECT=TRUE ../certi_src

This will create your normal project in the build tree and additionally an extra project in the source tree, we call it the "source-project". In Eclipse you can then import this source-project the same way as you import the normal project. This way you'll have two (or more) projects, one for browsing the sources and doing version control, the other for building your project.

Accessing the Source and Advanced Editing Features

Eclipse has advanced support for editing C/C++ sources, including code navigation, autocompletion etc. For that to work the sources must be inside the project (the additional source-project from above is not inside the project). The Eclipse project generator therefore creates a linked resource to the source tree in the Eclipse project. This makes the C/C++ features work.

This linked resource isn't created if the build directory is a subdirectory of the source directory because Eclipse doesn't allow to load projects which have linked resources pointing to a parent directory. So we recommend to create your build directories not as children, but as siblings to the source directory. E.g.:

/path/to/source
/path/to/build

Discussion about limitations

If you would like to monitor the changes to the EclipseCDT4 support, you can view the following links which contain the git history log for changes to the two main files:


Eclipse assumes project files (i.e. .project and .cproject) must be at the root of the project tree and a project may be handled by a versioning system (CVS, SVN, ...) iff the root project tree is.

This assumption clashes with the fact that CMake generated files should stay in the build tree whereas source files (which are usually those handled by a versioning system) reside in the source tree.

There has been a fair amount of discussion regarding this problem of the Eclipse CDT4 Generator:

  1. Trouble with CMake + Eclipse + SVN/CVS
  2. *Updated* Eclipse CDT4 CMake Generator - Pre-Alpha version
  3. Partially Shared project using Eclipse CDT (cdt-dev ML)


Manual Method

Create an Eclipse Project

Create an Eclipse CDT (C/C++ Development Tooling) project using the File > New > C++ Project command for your C++ project, or File > New > C Project for a C project.

Do not create the project using the CMake Eclipse project generator.

Create Eclipse Make Targets

The conventional approach to using CMake with Eclipse is to create an external tool in Eclipse. However, a Make Target is simpler, and because it is stored in the Eclipse .project file, you can check it into your version control system and it will work in every one of your working copies, on every computer.

Create a Make Target for each configuration that you want to build. Here I assume that you have the usual Release and Debug configurations:

  • Display the Make Target window using the Window > Show View > Make Target menu command. It should appear on the right, with the Outline window.
  • Select the folder for the project for which you want to add CMake. CMake will run with this folder as its working directory.
  • Right click on the folder and select New from the context menu. The Create Make Target dialog will appear.
    • Type Target name CMake Release
    • In Make target, deselect Same as the target name, and make sure that the Make target field is empty
    • In Build Command, deselect Use builder settings and set the Build command to
cmake -E chdir Release/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Release
  • Click OK
  • Repeat, this time for Target name CMake Debug, and Build command,
cmake -E chdir Debug/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Debug
  • Create the Release/ and Debug/ directories
mkdir Release Debug

Set Up the Eclipse CDT Builder

Next, set up the CDT builder to run the Makefiles that CMake builds.

  • Right click on a CDT project. In the context menu, select Properties.
  • On the left, select C/C++ Build
    • Set Configuration to Release
      • Choose the Builder Settings tab
        • Deselect Use default build command
        • Specify the Build command:
make -C ${ConfigName}
  • Deselect Generate Makefiles automatically
  • Make the Build directory field blank
  • Choose the Behavior tab
  • Select Build (Incremental build) and specify the target name all
  • Select Clean and specify the target name clean
  • Set Configuration to Debug
  • Choose the Builder Settings tab
  • Set all values exactly the same as the Release configuration
  • Choose the Behavior tab
  • Set all values exactly the same as the Release configuration
  • Click OK

Build the Project

Use CMake to generate an out-of-source GNU Make build system:

  • In the Make Targets window, double click on CMake Release or CMake Debug to generate the GNU Make build system in Release/ or Debug/, respectively
  • If necessary, edit your CMakeLists.txt control files
  • Delete the contents of the corresponding build directory. For example:
rm -r Release/*

and repeat.

Actually, for minor edits to your CMakeLists.txt control files, you need not delete the build directory. However, I cannot tell you exactly what the threshold for “minor edits” is.

Now, build the project the usual way with Eclipse:

  • Select the configuration to build (Release of Debug) with the Project > Build Configurations > Set Active command
  • Build with the Project > Build Project command
  • Edit your source code files, and repeat

Source

This information was directly taken from www.cmake.org andwww.voom.net Thank you for sharing with open-source license! This page pretends to be as a connection point for the author of this wiki (I'm just trying to avoid googling about it everytime I need this info :) )

<think>好的,我现在需要解决用户在ARM64-v8a平台上配置C/C++项目失败的问题。首先,用户提到之前引用了几个关于路径配置错误和平台设置的例子,比如误将PATH设置为32位的Qt路径,导致找不到正确的库,还有在配置Qt时指定了错误的平台参数。这可能意味着用户的项目配置中存在路径或平台参数不正确的问题。首先,我应该检查编译工具链是否正确。ARM64-v8a需要支持该架构的交叉编译工具链。用户可能没有正确安装或配置工具链,比如使用AndroidNDK的话,需要确认是否选择了正确的版本和工具链。例如,NDKr19之后推荐使用Clang而不是GCC,所以用户可能需要更新编译脚本中的编译器设置。接下来,查看CMake或Makefile中的平台配置。用户之前尝试配置Qt时指定了win32-msvc2005和wince平台,这显然不适用于ARM64-v8a。正确的做法是在CMake中设置-DCMAKE_SYSTEM_NAME=Android,-DCMAKE_ANDROID_ARCH=arm64-v8a等参数。同时,确保交叉编译的工具链文件正确指向NDK中的toolchain.cmake文件,比如使用NDK提供的android.toolchain.cmake。 然后,考虑操作系统检测宏的问题。用户引用中提到使用预定义宏来检测操作系统,这在跨平台编译中很重要。例如,在代码中检查是否定义了__aarch64__宏,以确保代码在ARM64环境下正确编译。如果项目中有平台相关的代码,需要确认这些宏是否正确启用,避免因条件编译错误导致配置失败。另外,依赖库的路径是否正确?用户之前错误地将PATH指向了32位的Qt路径,导致找到Anaconda的库。这可能意味着当前项目的库路径配置错误,需要检查链接器是否指向了ARM64-v8a的库文件,而不是x86或armv7的库。特别是在使用第三方库时,必须确保它们被编译为ARM64架构的版本。还要检查编译器的标志是否正确。ARM64-v8a可能需要特定的优化标志或指令集设置,比如-march=armv8-a。同时,检查是否有冲突的编译选项,比如混合使用了不同架构的选项。最后,考虑构建系统的配置。如果使用Autoconf,可能不适合Windows或跨平台项目,用户可能需要改用CMake来更好地管理不同平台的配置。确保CMake生成的文件正确指定了目标架构和工具链。 综合以上几点,用户需要逐步验证工具链、平台参数、依赖库路径、编译器标志和构建系统配置,确保每个环节都针对ARM64-v8a正确设置。同时,参考AndroidNDK的官方文档,确认最新的配置方法,避免因工具链过时而导致问题。</think>### ARM64-v8a平台C/C++配置失败解决方案 #### 1. 验证交叉编译工具链配置 ARM64-v8a需要专用的交叉编译工具链。使用Android NDK时需确认: - 使用NDK r21+版本(推荐LTS版本) - 在CMake中明确指定工具链文件: ```cmake set(CMAKE_TOOLCHAIN_FILE ${NDK_PATH}/build/cmake/android.toolchain.cmake) set(CMAKE_ANDROID_ARCH_ABI arm64-v8a) ``` 若使用自定义工具链,确保包含正确的编译器路径: ```bash # 示例GCC工具链配置 export CC=aarch64-linux-gnu-gcc export CXX=aarch64-linux-gnu-g++ ``` #### 2. 操作系统检测宏修正 在代码中使用正确的架构检测宏: ```cpp #if defined(__aarch64__) // ARM64-v8a专用宏 // ARM64优化代码路径 #endif ``` 避免使用过时的ARM宏如`__ARM_ARCH_7A__`[^3] #### 3. 依赖库兼容性验证 通过`readelf`检查第三方库架构: ```bash readelf -h libexample.so | grep Machine # 正确输出应包含"AArch64" ``` 若发现32位库混入,需重新编译依赖库: ```bash ./configure --host=aarch64-linux-android ``` #### 4. CMake关键参数配置 ```cmake # 强制指定目标架构 set(CMAKE_ANDROID_ARM_MODE arm64-v8a) # 禁用混合架构 set(CMAKE_ANDROID_ARM_NEON TRUE) # 设置API级别 set(CMAKE_SYSTEM_VERSION 21) ``` #### 5. 调试符号生成配置 ```cmake # 分离调试符号以减小包体积 set(CMAKE_BUILD_TYPE RelWithDebInfo) add_compile_options(-g0 -Wl,--strip-debug) ``` #### 6. 典型错误排查 - **错误表现**:`undefined reference to __atomic_fetch_add_8` - **解决方案**:添加链接参数`-latomic` ```cmake target_link_libraries(your_target PRIVATE atomic) ``` #### 7. 构建系统优化 推荐使用Ninja替代Make: ```bash cmake -GNinja -DCMAKE_MAKE_PROGRAM=ninja .. ``` #### 8. 环境验证脚本 创建架构验证脚本`verify_arm64.sh`: ```bash #!/bin/bash file ${YOUR_BINARY} | grep "ARM aarch64" objdump -d ${YOUR_BINARY} | grep -q "ldr x0" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值