我有一个项目,我正在使用 cmake 来帮助构建。假设项目在/home/proj,构建目录在/home/proj-build
该/home/proj目录是一个带有一些标签的 git 存储库,我想在构建代码时将最新的标签合并到代码中,以便二进制文件知道它是什么版本。
在我的CMakeLists.txt文件中,我在设置部分有以下节:
set(CMAKE_BUILD_TYPE None)
message(${CMAKE_CURRENT_SOURCE_DIR})
execute_process(COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && exec git describe --abbrev=0 --always OUTPUT_VARIABLE GIT_VERSION)
message("${GIT_VERSION}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__GIT_VERSION=\\\"${GIT_VERSION}\\\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__GIT_VERSION=\\\"${GIT_VERSION}\\\"")
Run Code Online (Sandbox Code Playgroud)
问题是复合命令execute_process显然为版本返回了一个空字符串。我必须以某种方式git describe从存储库目录中运行命令,但它与生成代码的构建目录不同。
我怎样才能让 make 进程在适当的目录中执行适当的 git 命令,即使是从构建目录中调用的?另外,我怎样才能cmake像通常在 shell 中期望的那样从复合命令中获取输出?
我尝试过的事情
我试图按照此处的建议将复合命令用括号括起来,以便在子 shell 中运行该命令,但输出似乎没有返回到父 shell。
此链接表明execute_process可以通过多次COMMAND调用运行,但似乎stdout从第一个调用到stdin下一个调用,因此不会产生所需的结果。
我还尝试sh -c在execute_process结构内部使用显式调用 shell 。我不确定我是否完全理解这次尝试的结果,但我无法获得'在这种情况下我需要在execute_process.
我正在尝试将通用 Windows 平台应用程序迁移到 CMake,但在构建时收到以下错误:
\src\windows-uwp\App.xaml : XamlCompiler error WMC1002: x:Class type 'langdetect.App' is not found in 'langdetect'
Run Code Online (Sandbox Code Playgroud)
我确实在迁移期间更改了命名空间的名称以匹配 CMake 项目名称,但我几乎可以肯定我已经更新了所有引用。所有的 C/C++ 代码都编译得很好。我正在使用 CMake 3.5.2 和 Visual Studio Community 2015。
这是 App.xaml:
<Application
x:Class="langdetect.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:langdetect"
RequestedTheme="Light">
</Application>
Run Code Online (Sandbox Code Playgroud)
应用程序.xaml.h:
#pragma once
#include "App.g.h"
namespace langdetect
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
ref class App sealed
{
protected:
virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;
internal:
App();
private:
void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
void …Run Code Online (Sandbox Code Playgroud) 我正在尝试一个 hello world 测试来在 Windows 下制作 cmake,使用 MinGW 作为编译器。
此答案建议cmake使用以下-G标志运行:
cmake -G "MinGW Makefiles" .
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做,我会收到一条消息,说那不是已知的生成器。实际上,运行cmake --help,在Generators部分下列出了以下生成器:
如您所见,未列出“MinGW Makefiles”。
如果这是相关的,我已经安装了 MinGW 并在我的系统上的常用文件夹中工作C:\MinGW。我还通过 WinBuilds 和 MSYS2 安装了 MinGW-w64,同样在默认安装文件夹中。我正在使用cmake version 3.5.2,通过 MSYS2 安装。
为什么“MinGW Makefiles”没有在生成器中列出?
我试图通过包含以下头文件在我的 C++ 程序中使用 ncurses:
#include <curses.h>
#include <menu.h>
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)
我正在使用 CLion IDE,这是我的 CMakeList.txt:
cmake_minimum_required(VERSION 3.6)
project(LearnC)
set(CURSES_USE_NCURSES TRUE)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(LearnC ${SOURCE_FILES})
target_link_libraries(LearnC ${CURSES_INCLUDE_DIR})
Run Code Online (Sandbox Code Playgroud)
编译进行得很顺利,但是在链接时出现以下错误:
[ 20%] Linking CXX executable LearnC
CMakeFiles/LearnC.dir/main.cpp.o: In function `main':
../LearnC/main.cpp:20: undefined reference to `initscr'
../LearnC/main.cpp:22: undefined reference to `clear'
../LearnC/main.cpp:23: undefined reference to `noecho'
../LearnC/main.cpp:24: undefined reference to `curs_set'
../LearnC/main.cpp:25: undefined reference to `cbreak'
../LearnC/main.cpp:26: undefined reference to `nl'
../LearnC/main.cpp:27: undefined reference to `stdscr'
../LearnC/main.cpp:27: undefined reference …Run Code Online (Sandbox Code Playgroud) 我正在尝试编译一个简单的程序来读取 HDF5 文件。代码使用 h5c++ 正确编译。但是我需要一个相同的 cmakelists.txt
读取数据文件
#include <iostream>
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "testfile.h5" );
int main (void)
{
H5File openFile( FILE_NAME, H5F_ACC_RDONLY );
}
Run Code Online (Sandbox Code Playgroud)
我为它尝试了一个 cmakelists 但它没有用。它给出了“未定义的错误”
readdata.cpp:(.text+0x1d): undefined reference to `H5::FileAccPropList::DEFAULT'
readdata.cpp:(.text+0x24): undefined reference to `H5::FileCreatPropList::DEFAULT'
readdata.cpp:(.text+0x38): undefined reference to `H5check_version'
readdata.cpp:(.text+0x54): undefined reference to `H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
readdata.cpp:(.text+0x60): undefined reference to `H5::H5File::~H5File()'
Run Code Online (Sandbox Code Playgroud)
CMakelists.txt
cmake_minimum_required(VERSION 3.1.0)
PROJECT (readhdf5)
find_package(HDF5 REQUIRED)
include_directories(${HDF5_INCLUDE_DIRS})
add_executable( readdata …Run Code Online (Sandbox Code Playgroud) 我的项目是这样的:
project
??? test
? ??? CMakeLists.txt
? ??? main.cpp
? ??? test_class1.cpp
??? CMakeLists.txt
??? main.cpp
??? ...
??? class1.h
??? class1.cpp
Run Code Online (Sandbox Code Playgroud)
我想重用为项目二进制文件编译的 class1.o。默认的 CMake 行为也对其进行了两次编译以进行测试。我尝试使用 OBJECT 库,但它把所有对象都放在这个库变量中。然后编译器打印
main.cpp:(.text.startup+0x0): `main' 的多重定义
这意味着在一个目标中有 2 个不同的 main.o。来自主项目编译的所有其他 *.o 文件都是。
如何排除不需要的 *.o 文件?
在 OSX 上使用 CLion,我正在创建一个简单的控制台应用程序,我想在其中执行system("clear"). 当我在 OSX 终端中运行该应用程序时,它可以正常工作。当我使用 CLion 终端运行它时,system("clear")失败并显示消息:
未设置 TERM 环境变量。
我首先尝试在我的cmakelists.txt文件中手动设置它,但没有成功
set(ENV{TERM} "xterm-256color")
Run Code Online (Sandbox Code Playgroud)
然后,我尝试使用以下键/值对(环境选项)在构建、执行、部署中的控制台和 CMake 部分的 CLion 设置中手动设置它(然后完全重新启动):
TERM xterm-256color
Run Code Online (Sandbox Code Playgroud)
在我的 OSX 终端中,env | grep TERM打印出来
TERM_PROGRAM_VERSION=388
TERM_PROGRAM=苹果_终端
TERM=xterm-256color
我错过了什么?可惜找不到了。。。
是否有任何命令可以放在 CMakeLists.txt 文件中以清除所有已定义的变量?如果我理解得很好,我可以有选择地清除它们对每个变量有选择地“取消设置”,但我需要做一些更像“取消设置(*)”的事情。
我有一个 C++ 项目,它由一个主程序 ( main.cpp)、一个定义抽象类的头文件 ( algorithm.hpp) 和一个algorithms/充满实现抽象类的类的子目录 ( ) 组成。我已将 CMake 配置为将子目录构建为对象库:
# algorithms/CMakeLists.txt
add_library(algorithms_lib OBJECT
algo1.cpp
algo2.cpp
# etc.
)
Run Code Online (Sandbox Code Playgroud)
在我的项目的根目录中,我使用这个对象库作为应用程序的源之一:
# Top-level CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(MyApp CXX)
add_subdirectory(algorithms)
add_executable(my-app
main.cpp
$<TARGET_OBJECTS:algorithms_lib>
)
Run Code Online (Sandbox Code Playgroud)
我的应用程序是用 C++14 编写的,所以我需要告诉 CMake 为此使用正确的编译选项。我不想要求 CMake 3.8 版(因为它没有打包在我想要支持的一些最近的 Linux 发行版中),所以我不能使用cxx_std_14编译功能;相反,我列出了一些我使用的个人语言功能:
# Top-level CMakeLists.txt (cont.)
target_compile_features(my-app PUBLIC
cxx_auto_type
cxx_constexpr
cxx_defaulted_functions
# ...14 more lines...
)
Run Code Online (Sandbox Code Playgroud)
问题是,这些功能仅适用于顶级my-app目标,而不适用于algorithms_lib目标,因此子目录中的源代码不会被编译为 C++14。
我知道我可以将整个大块复制target_compile_features到 中algorithms/CMakeLists.txt,但我宁愿不这样做——特别是因为这个例子被简化了,而且我实际上有九个这样的子目录,每个子目录都构建了自己的对象库。那将是大量重复的样板代码。
有没有办法为项目中的所有 …
我下载了 nitroshare.tar.gz 文件,因为我想将它安装在我的 kali linux 上。我按照github页面上的说明进行安装并下载了安装所需的所有软件包。
当我使用 cmake 命令时,它向我显示此错误
未知的 cmake 命令 qt5_wrap_ui
这是 CMakeList.txt 文件
cmake_minimum_required(VERSION 3.7)
configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/config.h")
set(SRC
application/aboutdialog.cpp
application/application.cpp
application/splashdialog.cpp
bundle/bundle.cpp
device/device.cpp
device/devicedialog.cpp
device/devicelistener.cpp
device/devicemodel.cpp
icon/icon.cpp
icon/trayicon.cpp
settings/settings.cpp
settings/settingsdialog.cpp
transfer/transfer.cpp
transfer/transfermodel.cpp
transfer/transferreceiver.cpp
transfer/transfersender.cpp
transfer/transferserver.cpp
transfer/transferwindow.cpp
util/json.cpp
util/platform.cpp
main.cpp
)
if(WIN32)
set(SRC ${SRC} data/resource.rc)
endif()
if(APPLE)
set(SRC ${SRC}
data/icon/nitroshare.icns
transfer/transferwindow.mm
)
set_source_files_properties("data/icon/nitroshare.icns" PROPERTIES
MACOSX_PACKAGE_LOCATION Resources
)
endif()
if(QHttpEngine_FOUND)
set(SRC ${SRC}
api/apihandler.cpp
api/apiserver.cpp
)
endif()
if(APPINDICATOR_FOUND)
set(SRC ${SRC}
icon/indicatoricon.cpp
)
endif()
qt5_wrap_ui(UI
application/aboutdialog.ui
application/splashdialog.ui
device/devicedialog.ui
settings/settingsdialog.ui
transfer/transferwindow.ui
) …Run Code Online (Sandbox Code Playgroud)