用于 c++0x 原子和线程的 GCC 标志
GCC flags for c++0x atomics and threads
我试图编译这个C++原子和线程的基本示例,尽管当我编译主文件时.cpp gcc会抛出一些std lib错误 - 这似乎与我的代码无关。
主.cpp
#include <thread>
#include <atomic>
#include <stdio.h>
#include "randomdelay.h"
using namespace std;
atomic<int> flag;
int sharedValue = 0;
RandomDelay randomDelay1(1, 60101);
RandomDelay randomDelay2(2, 65535);
void IncrementSharedValue10000000Times(RandomDelay& randomDelay)
{
int count = 0;
while (count < 10000000)
{
randomDelay.doBusyWork();
int expected = 0;
if (flag.compare_exchange_strong(expected, 1, memory_order_relaxed))
{
// Lock was successful
sharedValue++;
flag.store(0, memory_order_relaxed);
count++;
}
}
}
void Thread2Func()
{
IncrementSharedValue10000000Times(randomDelay2);
}
int main(int argc, char* argv[])
{
printf("is_lock_free: %sn", flag.is_lock_free() ? "true" : "false");
for (;;) {
sharedValue = 0;
thread thread2(Thread2Func);
IncrementSharedValue10000000Times(randomDelay1);
thread2.join();
printf("sharedValue=%dn", sharedValue);
}
return 0;
}
我正在使用的完整代码:https://github.com/preshing/AcquireRelease
以下是 GCC 错误消息:
[lewis@localhost preshing-AcquireRelease-1422872]$ g++ -std=c++0x -pthread main.cpp
/tmp/cc95LElq.o: In function `IncrementSharedValue10000000Times(RandomDelay&)':
main.cpp:(.text+0xdd): undefined reference to `RandomDelay::doBusyWork()'
/tmp/cc95LElq.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x23d): undefined reference to `RandomDelay::RandomDelay(int, int)'
main.cpp:(.text+0x251): undefined reference to `RandomDelay::RandomDelay(int, int)'
collect2: error: ld returned 1 exit status
这是我使用的命令:g++ -std=c++0x -pthread main.cpp
RandomDelay
类似乎是在randomdelay.cpp
中实现的。您必须编译此文件并将其与main.cpp
链接在一起。例如:
$ g++ -std=c++0x -pthread -o program_name main.cpp randomdelay.cpp
>你需要添加包含你的RandomDelay
定义的cpp文件..ie类似于g++ -std=c++0x -pthread main.cpp randomdelay.cpp
相关文章:
- 如何使用 gnu gcc 标志 -mpc32、-mpc64 和 -mpc80?
- 如何告诉 gcc 显示您使用的优化标志列表
- 为什么 gcc 编译器标志未知?
- gcc '-rdynamic' 标志等效于 MSVC
- CMake还将所有gcc标志传递给nvcc
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- 是否有 GCC 标志来发出有关作用域内相同变量重定义的警告?
- 是否有编译器标志可以使较新的 gcc 版本像旧版本一样构建
- 为什么一些 GCC 警告标志不属于C++语言,但在C++中工作
- ms-extensions标志对gcc到底有什么作用?
- Atom 和 i3/5 之间的通用 gcc C/C++ 标志?
- GCC 版本/标志和 FAM
- GCC内联ASM X86 CPU标志作为输入依赖性
- GCC - 包含编译标志的宏
- 如何指定 gcc 标志 (CXXFLAGS) 特别是对于特定模块
- 用于 c++0x 原子和线程的 GCC 标志
- 通过GCC标志-std=c++11 CXXFLAGS或CXX或CFLAGS
- 用于 G++ 调用的等效 GCC 标志
- 使用-g gcc标志编译的程序比不使用-g编译的程序慢
- -static-libstdc++ GCC标志会影响Linux上生成静态库(*.a)吗?