发现Clion也能进行远程调试了。
真是与时俱进的呢~
按照这个官方教程来进行配置。
由于我的远程机器CMake/GCC/G++/GDB版本都比较低(CLion要求的版本都比较高),而且没有sudu权限,所以就用conda新建了一整套环境:
conda create --name my_workspace
conda activate my_workspace
conda install -c conda-forge binutils cmake make
conda install -c conda-forge gdb # 这个有可能会安不上
然后CLion中制定env的lib路径即可。
配置好之后,发现HelloWorld本地编译OK,远端编译失败。看Log好像是由于pthread导致的。那么修改CMakeLists如下:
cmake_minimum_required(VERSION 3.14)
project(CPPPlayground)
set(CMAKE_CXX_STANDARD 17)
find_package (Threads)
add_executable(CPPPlayground main.cpp)
target_link_libraries (CPPPlayground ${CMAKE_THREAD_LIBS_INIT})
然后测试代码如下
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>
#include <sys/utsname.h>
struct Base {
Base() { std::cout << " Base::Base()\n"; }
// 注意:此处非虚析构函数 OK
~Base() { std::cout << " Base::~Base()\n"; }
};
struct Derived : public Base {
Derived() { std::cout << " Derived::Derived()\n"; }
~Derived() { std::cout << " Derived::~Derived()\n"; }
};
void thr(std::shared_ptr<Base> p) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::shared_ptr<Base> lp = p; // 线程安全,虽然自增共享的 use_count1
{
static std::mutex io_mutex;
std::lock_guard<std::mutex> lk(io_mutex);
std::cout << "local pointer in a thread:\n"
<< " lp.get() = " << lp.get()
<< ", lp.use_count() = " << lp.use_count() << '\n';
}
}
int main() {
// 测试代码编译环境
struct utsname name;
std::cout << "I am on "
#ifdef __linux__
<< "Linux"
#elif __APPLE__
<<"Mac OS"
#endif
<< name.release << std::endl;
// 测试thread和C++11标准
std::shared_ptr<Base> p = std::make_shared<Derived>();
std::cout << "Created a shared Derived (as a pointer to Base)\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
std::thread t1(thr, p), t2(thr, p), t3(thr, p);
p.reset(); // 从 main 释放所有权
std::cout << "Shared ownership between 3 threads and released\n"
<< "ownership from main:\n"
<< " p.get() = " << p.get()
<< ", p.use_count() = " << p.use_count() << '\n';
t1.join();
t2.join();
t3.join();
std::cout << "All threads completed, the last one deleted Derived\n";
}
发表回复