【问题标题】:Undefined symbols for architecture x86_64 mac os library Clion error架构 x86_64 mac os 库 Clion 错误的未定义符号
【发布时间】:2021-10-03 22:40:24
【问题描述】:

我正在尝试构建我的库并将其链接到项目。但我得到一个错误。编译器看不到 F::method() 的实现; 6.cpp 中的函数。为什么?谁能解释错误是什么?

错误

====================[ Build | lesson6 | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mbp15/CLionProjects/lesson6/cmake-build-debug --target lesson6 -- -j 6
Scanning dependencies of target lesson6
[ 50%] Building CXX object CMakeFiles/lesson6.dir/app/main.cpp.o
[100%] Linking CXX executable lesson6
Undefined symbols for architecture x86_64:
  "F::method(int, int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [lesson6] Error 1
make[2]: *** [CMakeFiles/lesson6.dir/all] Error 2
make[1]: *** [CMakeFiles/lesson6.dir/rule] Error 2
make: *** [lesson6] Error 2

Cmake.h

cmake_minimum_required(VERSION 3.17)

project(lesson6 VERSION 1.0.0 DESCRIPTION "Lesson project" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)

add_executable(lesson6 app/main.cpp)

add_library(MyLibrary include/lesson6.h src/lesson6.cpp)

target_link_libraries(lesson6 PUBLIC ${MyLibrary})

ma​​in.cpp

#include "../include/lesson6.h"

int main(){

    F object;

    object.method(1,2);

    return 0;
}

lesson6.h

#ifndef LESSON6_LESSON6_H
#define LESSON6_LESSON6_H

class F {

public:

    int method(int one, int two);

};

#endif //LESSON6_LESSON6_H

lesson6.cpp

#include "../include/lesson6.h"

int F::method(int one, int two) {
    return one + two;
}

Project tree

【问题讨论】:

    标签: c++ macos clang clion clang++


    【解决方案1】:

    您无需在 CMakeLists.txt 中取消引用库目标。

    尝试将最后一行更改为

    target_link_libraries(lesson6 PUBLIC MyLibrary)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-10
      • 2017-12-03
      • 1970-01-01
      • 2012-04-26
      • 2017-08-21
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多