gcc 4.9.0 安装后,无法编译 C11程序,能编译 C++2011,缺 threads.h

hzhxxx 2014-06-05 04:11:50
gcc 4.9.0 安装后,无法编译 C11程序,能编译 C++2011,缺 threads.h
详细情况见:
http://blog.csdn.net/hzhxxx/article/details/28634893

代码:
3.6. 验证编译功能(C11),缺少 threads.h文件,编译失败
创建文件test_gcc4.9.c,内容如下:
#include <string.h>
#include <stdio.h>
#include <threads.h>

void * fun(void *data)
{
printf("dddd\n");
return 0;
}

int main(int argc,char *argv[])
{
thrd_t loc;
thrd_create(&loc, fun,0);
thrd_join(loc,0);
return 0;
}

编译执行,注意看参数 -std=c11,-lgcc_s,证明是使用新标准
查看链接的动态库,是新的库
呜呼,无法编译成功,无法执行正常!!!
#/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread;
#test_gcc4.9.c:4:21: 致命错误:threads.h:没有那个文件或目录
#include <threads.h>
^
编译中断。
...全文
1059 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
c语言由于没有命名空间,标准库里的线程库虽然来源于pthread,但是名称不一样。 我建议是你先按pthread来命名,然后等c11标准支持之后,再#define过来。
zh1599512 2014-12-27
  • 打赏
  • 举报
回复
theads.h只是 建议标准 LLVM都不支持的。
赵4老师 2014-06-13
  • 打赏
  • 举报
回复
语法糖越甜,编译调试查错越苦! 把有限的生命浪费在品尝/品鉴无穷多种的语法糖中,我认为不值当。
hzhxxx 2014-06-13
  • 打赏
  • 举报
回复
最新的 glibc 2.19 和 glibc 2.17(也就是红帽子昨天发布的版本携带的) 也没有发现这个 threads.h 真是啧啧怪事啊!!!
超级能量泡泡 2014-06-11
  • 打赏
  • 举报
回复
引用 24 楼 hzhxxx 的回复:
创建文件test_gcc4.9.c,内容如下: #include <string.h> #include <stdio.h> //注意这行,路径不一致 #include <thr/threads.h> //注意这个,函数定义必须是 //typedef int(*_Thrd_start_t)(void *); int fun(void *data) { printf("thread=%d\n", thrd_current()); return 0; } int main(int argc,char *argv[]) { thrd_t loc; thrd_create(&loc, fun,0); thrd_join(loc,0); return 0; } 编译执行,注意看参数 -std=c11,-lgcc_s,证明是使用新标准 查看链接的动态库,是新的库 呜呼,无法编译成功,无法执行正常!!! #/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread; #test_gcc4.9.c:4:21: 致命错误:threads.h:没有那个文件或目录 #include <threads.h> ^ 编译中断。 后记: 安装免费的 visual studio express 2013 后,正确编译后运行正常; 看来,学习 c++ 2011 或者 C11 的选择还是 vs2013靠谱。
C11不如C++11有意思。
hzhxxx 2014-06-11
  • 打赏
  • 举报
回复
创建文件test_gcc4.9.c,内容如下: #include <string.h> #include <stdio.h> //注意这行,路径不一致 #include <thr/threads.h> //注意这个,函数定义必须是 //typedef int(*_Thrd_start_t)(void *); int fun(void *data) { printf("thread=%d\n", thrd_current()); return 0; } int main(int argc,char *argv[]) { thrd_t loc; thrd_create(&loc, fun,0); thrd_join(loc,0); return 0; } 编译执行,注意看参数 -std=c11,-lgcc_s,证明是使用新标准 查看链接的动态库,是新的库 呜呼,无法编译成功,无法执行正常!!! #/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread; #test_gcc4.9.c:4:21: 致命错误:threads.h:没有那个文件或目录 #include <threads.h> ^ 编译中断。 后记: 安装免费的 visual studio express 2013 后,正确编译后运行正常; 看来,学习 c++ 2011 或者 C11 的选择还是 vs2013靠谱。
hzhxxx 2014-06-11
  • 打赏
  • 举报
回复
我也认为这个是不是会造成 分裂,嘿嘿
神-气 2014-06-10
  • 打赏
  • 举报
回复
引用 13 楼 mymtom 的回复:
是thread吧,不是thread.h啊

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
 
std::mutex g_display_mutex;
 
void foo()
{
    std::thread::id this_id = std::this_thread::get_id();
 
    g_display_mutex.lock();
    std::cout << "thread " << this_id << " sleeping...\n";
    g_display_mutex.unlock();
 
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::thread t1(foo);
    std::thread t2(foo);
 
    t1.join();
    t2.join();
}
++ , 楼主 , C++中标准库都是没有.h后缀的。
mymtom 2014-06-10
  • 打赏
  • 举报
回复
是thread吧,不是thread.h啊

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
 
std::mutex g_display_mutex;
 
void foo()
{
    std::thread::id this_id = std::this_thread::get_id();
 
    g_display_mutex.lock();
    std::cout << "thread " << this_id << " sleeping...\n";
    g_display_mutex.unlock();
 
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::thread t1(foo);
    std::thread t2(foo);
 
    t1.join();
    t2.join();
}
hzhxxx 2014-06-10
  • 打赏
  • 举报
回复
vs2013 express 安装完成后,终于把例子运行起来了!!!很多差异,大家看看 C11。 #include <map> //注意这行,路径不一致 #include <thr/threads.h> #include <string> #include <stdio.h> #include <iostream> using namespace std; //注意这个,函数定义必须是 //typedef int(*_Thrd_start_t)(void *); int f(void * data) { printf("thread=%d\n", thrd_current()); std::map<std::string, std::string> m; m.insert(std::make_pair("a1", "b2")); m.insert(std::make_pair("a2", "b1")); for (auto iter = m.begin(); iter != m.end(); ++iter) { std::cout << "key=" << iter->first << ",value=" << iter->second << endl; } return 0; } int _tmain(int argc, _TCHAR* argv[]) { thrd_t t; //typedef int (*_Thrd_start_t)(void *); thrd_create(&t, f, 0); thrd_join(t,0); system("pause"); return 0; }
hzhxxx 2014-06-10
  • 打赏
  • 举报
回复
引用 21 楼 daiweifeng 的回复:
我的Visual Studio Express 2013里倒是有支持threads.h库,看来是gcc自己的问题
错误 1 error C1083: 无法打开包括文件: “threads.h”: No such file or directory d:\cpp11\consoleapplication1\consoleapplication1\consoleapplication1.cpp 8 1 ConsoleApplication1 我安装的 vs2013 express 没有这个文件啊,悲剧鸟??
超级能量泡泡 2014-06-10
  • 打赏
  • 举报
回复
我的Visual Studio Express 2013里倒是有支持threads.h库,看来是gcc自己的问题
超级能量泡泡 2014-06-10
  • 打赏
  • 举报
回复
引用 19 楼 hzhxxx 的回复:
按C11标准,线程库确实是 threads.h
那就是现在没有编译器完整支持这东西。我的4.8.1也没有这东西。
hzhxxx 2014-06-10
  • 打赏
  • 举报
回复
按C11标准,线程库确实是 threads.h
超级能量泡泡 2014-06-10
  • 打赏
  • 举报
回复
引用 17 楼 hzhxxx 的回复:
[quote=引用 16 楼 daiweifeng 的回复:] 楼主先确定到底是C语言还是C++语言。 编译时使用g++作编译程序,参数使用-std=c++11。C++和C的默认头文件是不一样的。
标题上说明很清楚吧。 编译也是用的 gcc,链接也是 libgcc,不是 libstdc++.so啊 #/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread; [/quote] C语言里没有threads.h这个头文件,C++里有thread这个头文件。 你想用C++11标准的线程库的话当然要用C++标准编译器咯。 或者你换成pthread.h试试看。
hzhxxx 2014-06-10
  • 打赏
  • 举报
回复
引用 16 楼 daiweifeng 的回复:
楼主先确定到底是C语言还是C++语言。 编译时使用g++作编译程序,参数使用-std=c++11。C++和C的默认头文件是不一样的。
标题上说明很清楚吧。 编译也是用的 gcc,链接也是 libgcc,不是 libstdc++.so啊 #/usr/local/gcc-4.9.0/bin/gcc -Wwrite-strings -std=c11 test_gcc4.9.c -o test_gcc4.9 -I/usr/local/gcc-4.9.0/include -L/usr/local/gcc-4.9.0/lib64 -lgcc_s -lpthread;
super_admi 2014-06-10
  • 打赏
  • 举报
回复
传说,貌似,可能,GCC对C11标准支持不完整吧?
超级能量泡泡 2014-06-10
  • 打赏
  • 举报
回复
楼主先确定到底是C语言还是C++语言。 编译时使用g++作编译程序,参数使用-std=c++11。C++和C的默认头文件是不一样的。
超级能量泡泡 2014-06-10
  • 打赏
  • 举报
回复
-std=c++11 #include<thread> 必须是C++11不是C11,一般现在只有C99,C11和以后的可能更多的用于异构计算
UniverseSpace007 2014-06-09
  • 打赏
  • 举报
回复
我记得用的是pthread.h这个头文件。。。
加载更多回复(10)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧