如何解决:编译器不会收到CMake定义的宏

时间:2019-05-28 08:31:37

标签: c++ cmake symbols

已解决! = ,而不是 -中CMakeLists.txt中的 say-hello < strong>目录

我在根据布尔向量的CMake教程https://www.youtube.com/watch?v=SYgESCQeGJY&list=PLK6MXr8gasrGmIiSuVQXpfFuE1uPT615s&index=8用CMake定义预处理器变量时遇到问题。 显然,我使用的视频版本与他在视频中使用的版本不同。但是在更改版本之前,我想在这里解决问题。

通过cmake ..成功配置后,我运行make并收到以下消息:

[ 25%] Building CXX object say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o
<command-line>:0:14: warning: ISO C++11 requires whitespace after the macro name
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp: In function ‘void hello::say()’:
<command-line>:0:17: error: expected ‘;’ before numeric constant
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp:7:43: note: in expansion of macro ‘HELLO_VERSION’
         std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
                                           ^~~~~~~~~~~~~
say-hello/CMakeFiles/say-hello.dir/build.make:62: recipe for target 'say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o' failed
make[2]: *** [say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o] Error 1
CMakeFiles/Makefile2:90: recipe for target 'say-hello/CMakeFiles/say-hello.dir/all' failed
make[1]: *** [say-hello/CMakeFiles/say-hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我正在使用:

  • Linux
  • g ++ 8.1
  • C ++ 11
  • CMake 3.14.4
  • Visual Studio代码

顶部CMakeLists.txt

cmake_minimum_required(VERSION 3.14.4)
project(MyProject VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 11)

add_subdirectory(say-hello)
add_subdirectory(mainsrc)

mainsrc中的CMakeLists.txt

add_executable(main.out main.cpp)

target_link_libraries(main.out PRIVATE say-hello)

main.cpp

#include <iostream>
#include <say-hello/hello.hpp>

int main(){
    hello::say();
    return 0;
}

说声你好,CMakeLists.txt

add_library(say-hello
            src/say-hello/hello.cpp
            src/say-hello/hello.hpp
            )

target_include_directories(say-hello PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")

target_compile_definitions(say-hello PUBLIC HELLO_VERSION-4)

hello.hpp

#ifndef HELLO
#define HELLO

//#define SAYHELLOVERSION 4

namespace hello{
    void say();
}

#endif

hello.cpp

#include <iostream>
#include "hello.hpp"

namespace hello{
    void say(){
        //int version = SAYHELLOVERSION;
        std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
    }
}

0 个答案:

没有答案