我正在尝试构建我的库并将其链接到项目。但我得到一个错误。编译器看不到 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})
main.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;
}