我在 M1 Pro Mac (CLion) 上構建,編譯時出現以下錯誤:
FAILED: tree
: && /Library/Developer/CommandLineTools/usr/bin/c -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/tree.dir/main.cpp.o CMakeFiles/tree.dir/Node.cpp.o CMakeFiles/tree.dir/Tree.cpp.o CMakeFiles/tree.dir/Unit.cpp.o -o tree && :
Undefined symbols for architecture arm64:
"Node<Unit>::addRoot(Unit)", referenced from:
_main in main.cpp.o
"Node<Unit>::Node()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
我有三個類Node,Tree和Unit。我遇到的大多數帖子都表明其中一個檔案未包含在專案中(例如,使用 g 編譯器的架構 arm64 的未定義符號)。我懷疑這是問題所在,因為它們似乎都包含在編譯器行中。
我的CMakeLists.txt看起來像這樣:
cmake_minimum_required(VERSION 3.21)
project(tree)
set(CMAKE_CXX_STANDARD 14)
add_executable(tree main.cpp Node.cpp Node.h Tree.cpp Tree.h Unit.cpp Unit.h)
我正在為Unit該類添加頭檔案,因為這似乎是冒犯編譯器的那個,但是我懷疑這就是問題所在。猜測這是一些make檔案設定?
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Node {
weak_ptr<Node> parent;
vector<shared_ptr<shared_ptr<Node>>> children;
T data;
public:
Node();
Node(weak_ptr<Node> parent, T &data);
void addRoot(T node_data);
};
并且有可能在這篇文章中過于冗長,這里是CMakeError.log輸出:
Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: /Library/Developer/CommandLineTools/usr/bin/cc
Build flags:
Id flags:
The output was:
1
ld: library not found for -lSystem
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: /Library/Developer/CommandLineTools/usr/bin/c
Build flags:
Id flags:
The output was:
1
ld: library not found for -lc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果我運行輸出c main.cpp Node.cpp Tree.cpp Unit.cpp:
In file included from main.cpp:3:
./Node.h:16:38: error: a space is required between consecutive right angle brackets (use '> >')
vector<shared_ptr<shared_ptr<Node>>> children;
^~
> >
main.cpp:7:5: warning: 'auto' type specifier is a C 11 extension [-Wc 11-extensions]
auto unit = std::make_unique<Unit>(1, "Test Unit");
^
main.cpp:7:22: error: no member named 'make_unique' in namespace 'std'
auto unit = std::make_unique<Unit>(1, "Test Unit");
~~~~~^
main.cpp:7:34: error: 'Unit' does not refer to a value
auto unit = std::make_unique<Unit>(1, "Test Unit");
^
./Unit.h:11:7: note: declared here
class Unit {
^
main.cpp:7:40: warning: expression result unused [-Wunused-value]
auto unit = std::make_unique<Unit>(1, "Test Unit");
^
main.cpp:8:20: error: expected ';' at end of declaration
Node<Unit> node {};
^
;
2 warnings and 4 errors generated.
In file included from Node.cpp:5:
./Node.h:16:38: error: a space is required between consecutive right angle brackets (use '> >')
vector<shared_ptr<shared_ptr<Node>>> children;
^~
> >
1 error generated.
我是 C 的新手,所以我發現這個問題有點難以追蹤。
uj5u.com熱心網友回復:
看起來您在使用模板時遇到了問題。在模板化類中,您必須要么將所有方法的實作都放在頭檔案中,要么在定義它的 cpp 主體中對具體類進行顯式模板實體化。
舉個例子更容易理解。在以下情況下,我將在標題中添加建構式的主體,因此我不必做任何其他事情
/////////////////////////////////////////
// In Node.h
/////////////////////////////////////////
template< typename T >
struct Node {
Node();
};
template< typename T >
Node<T>::Node() {
}
/////////////////////////////////////////
// In Node.cpp
/////////////////////////////////////////
// (nothing)
/////////////////////////////////////////
// Int main.cpp
/////////////////////////////////////////
#include "Node.h"
int main() {
Node<int> n;
}
在第二種情況下,我將Node<int>在 Node.cpp 的主體中對具體類進行顯式模板實體化,這樣我就不需要公開模板化類的實作。
/////////////////////////////////////////
// In Node.h
/////////////////////////////////////////
template< typename T >
struct Node {
Node();
};
/////////////////////////////////////////
// In Node.cpp
/////////////////////////////////////////
template< typename T >
Node<T>::Node() {}
// Explicit initialization
template struct Node<int>;
/////////////////////////////////////////
// Int main.cpp
/////////////////////////////////////////
#include "Node.h"
int main() {
Node<int> n;
}
我更喜歡使用第二種方法,因為它可以顯著降低構建時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/397256.html
上一篇:默認成員值初始化在GCC中被忽略
下一篇:根據模板引數以不同方式多載運算子
