我正在编写一个教程,并且取得了很大的进步,直到我遇到了最困难的部分。本教程是使用 CMake:Hello World 示例。最后也是最难的部分是“分别构建库和程序”部分。我已经在 Eclipse 中创建了这两个项目,并一直按照说明进行操作。作者暗示,要使 CMake 构建成功,还有更多工作要做。同时,我想让两个项目在 Eclipse 中相互协作。
当我尝试构建所有内容时,我收到此错误:
12:25:45 **** Build of configuration Debug for project Hello ****
make all
Building file: ../src/hello/Hello.cpp
Invoking: GCC C++ Compiler
g++ -I"/Users/pdl/Development/HelloWorld/Namer/src/namer" -include"/Users/pdl/Development/HelloWorld/Namer/src/namer/World.h" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello/Hello.d" -MT"src/hello/Hello.d" -o "src/hello/Hello.o" "../src/hello/Hello.cpp"
Finished building: ../src/hello/Hello.cpp
Building target: Hello
Invoking: MacOS X C++ Linker
g++ -L"/Users/pdl/Development/HelloWorld/Namer" -o "Hello" ./src/hello/Hello.o -l"/Users/pdl/Development/HelloWorld/Namer/src/namer/World.h"
ld: library not found for -l/Users/pdl/Development/HelloWorld/Namer/src/namer/World.h
collect2: ld returned 1 exit status
make: *** [Hello] Error 1
12:25:45 Build Finished (took 161ms)
问题是:
4 duplicate symbols for architecture x86_64
library not found for -l/Users/pdl/Development/HelloWorld/Namer/src/namer/World.h
make: ***[Hello] Error 1
make: ***[Namer] Error 1
当我尝试仅构建库项目 (Namer) 时,我收到以下错误:
12:36:11 **** Incremental Build of configuration Debug for project Namer ****
make all
Building target: Namer
Invoking: MacOS X C++ Linker
g++ -o "Namer" ./src/namer/CMakeFiles/2.8.11.2/CompilerIdCXX/CMakeCXXCompilerId.o ./src/namer/CMakeFiles/2.8.11.2/CompilerIdC/CMakeCCompilerId.o ./src/namer/World.o
duplicate symbol _main in:
./src/namer/CMakeFiles/2.8.11.2/CompilerIdCXX/CMakeCXXCompilerId.o
./src/namer/CMakeFiles/2.8.11.2/CompilerIdC/CMakeCCompilerId.o
duplicate symbol _info_compiler in:
./src/namer/CMakeFiles/2.8.11.2/CompilerIdCXX/CMakeCXXCompilerId.o
./src/namer/CMakeFiles/2.8.11.2/CompilerIdC/CMakeCCompilerId.o
duplicate symbol _info_platform in:
./src/namer/CMakeFiles/2.8.11.2/CompilerIdCXX/CMakeCXXCompilerId.o
./src/namer/CMakeFiles/2.8.11.2/CompilerIdC/CMakeCCompilerId.o
duplicate symbol _info_arch in:
./src/namer/CMakeFiles/2.8.11.2/CompilerIdCXX/CMakeCXXCompilerId.o
./src/namer/CMakeFiles/2.8.11.2/CompilerIdC/CMakeCCompilerId.o
ld: 4 duplicate symbols for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Namer] Error 1
12:36:11 Build Finished (took 61ms)
问题是:
4 duplicate symbols for architecture x86_64
make: ***[Namer] Error 1
有没有人能胜任帮助我的任务?这应该是一个简单的 HelloWorld 示例,其中 Hello 部分来自主项目,名称来自 Library 项目。对于了解在 Eclipse 中配置多个 C++ 项目的人来说,这可能很容易。也许 CMake 正在与 Eclipse 作战,我不知道。
预先感谢您的帮助。
--------------------------------------------源代码---- ------------------------------
好的。对于 Hello 项目,有一个 CMakeLists.txt 文件和一个 FindNamer.cmake 文件。
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.6)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
find_package(Namer REQUIRED)
include_directories("${Namer_INCLUDE-DIRS}")
add_executable(hello main.cpp)
target_link_libraries(hello ${Namer_LIBRARIES})
FindNamer.cmake
find_path(Namer_INCLUDE_DIRS World.h /usr/include "$ENV{NAMER_ROOT}")
find_library(Namer_LIBRARIES namer /usr/lib "$ENV{NAMER_ROOT}")
set(Namer_FOUND TRUE)
if (NOT Namer_INCLUDE_DIRS)
set(Namer_FOUND FALSE)
endif (NOT Namer_INCLUDE_DIRS)
if (NOT Namer_LIBRARIES)
set(Namer_FOUND FALSE)
endif (NOT Namer_LIBRARIES)
和Hello.cpp文件:
#include <stdio.h>
#include "World.h"
int main() {
printf("Hello %s\n", getWorld().c_str());
return 0;
}
Namer 项目有一个CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8.6)
project(Namer)
include_directories("${CMAKE_SOURCE_DIR}")
add_library(namer World.cpp World.h)
这是World.cpp:
#include "World.h"
std::string getWorld() { return "Earth"; }
这是World.h:
#include <string>
std::string getWorld();
如果您还有其他需要,请告诉我。再次感谢你。