【发布时间】:2019-12-22 05:21:06
【问题描述】:
我需要在我的程序中使用外部库。 (https://github.com/davidmoreno/onion) 我是 CMake 新手(以及这种编译程序的方式)。
该库包含CMakeLists.txt 文件,所以我想我应该可以使用它。
但是当我尝试编译时,例如hello world 程序示例 (/onion/examples/hello/)。我收到 CMake 无法链接(甚至找不到)使用的库(如 onion.h)的错误。
为了编译,我创建了一个子目录“build”,我从该子目录中执行命令cmake ..,然后是cmake --build .
这里是hello world程序的源文件。 (https://github.com/davidmoreno/onion/tree/master/examples/hello)
CMakeLists.txt:
include_directories (${PROJECT_SOURCE_DIR}/src/)
add_executable(hello hello.c)
target_link_libraries(hello onion)
hello.c(仅标头):
#include <onion/onion.h>
#include <onion/log.h>
#include <onion/version.h>
#include <signal.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
这是构建输出:
open# cd build/
open# cmake ..
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.10)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user1/onion/examples/hello/build
open# cmake --build .
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/hello.o
/home/user1/onion/examples/hello/hello.c:18:10: fatal error: 'onion/onion.h'
file not found
#include <onion/onion.h>
^~~~~~~~~~~~~~~
1 error generated.
*** Error 1 in . (CMakeFiles/hello.dir/build.make:63 'CMakeFiles/hello.dir/hello.o': /usr/bin/cc -I/home/user1/onion/examples/hello/src -...)
*** Error 1 in . (CMakeFiles/Makefile2:68 'CMakeFiles/hello.dir/all')
*** Error 1 in /home/user1/onion/examples/hello/build (Makefile:84 'all')
我尝试了很多解决方案,但没有一个对我有用。
感谢您的帮助。
【问题讨论】:
-
请在您的问题中提供所有相关代码。避免指向外部网站的链接,因为它们将来可能会更改或被删除。
-
是的,你是对的。我已经添加了两个源文件。
-
哪种语言,C 或 C++,因为它们是两种不同的语言? C++ 允许函数和运算符的重载。编译器在处理库时可能会使用 name mangling。
-
嗨,Thomas,这个例子是用 C 语言编写的,但是有一个 C++ 例子,我遇到了同样的问题。
-
No cmake_minimum_required command is present. A line of code such as cmake_minimum_required(VERSION 3.10) should be added at the top of the file来自自述文件if you want to use the examples on your instalation, compile and __install__ libonion for the current system first.您是否先为当前系统安装了 libonion?
标签: c++ c unix cmake compilation