解决编译C++程序时to_string()未声明等相关问题&Dev C++的undefined reference to `__imp_htons‘或codeclock的undefined refer

当用dev c++编译时会报 [Error] 'to_string' was not declared in this scope

当用code::Blocks编译时会报   to_string' was not declared in this scope

原因一样,均是因为编译器不支持C++11标准,所以解决方法就是使其能够支持该标准。

具体解决方案如下:

对于dev-C++

tool-Compiler Options

勾选Add the following commands when calling the comoiler选项,然后在输入框中输入:-std=c++11。即可!

对于Code::Blocks

setting-compiler

如图,勾选C++11标准即可!

(30条消息) 解决编译C++程序时to_string()未声明等相关问题_nodarkness的博客-CSDN博客

实在是忍不住吐槽,确实是花费了很多时间来解决,度娘搜索有时候差一点关键字就搜不到想要的(准确是差十万八千里)。总结一下吧,上面两个问题我都遇到了,同一段代码不同编辑器,类似的是都是显示未定义相关。未引入头文件的缘故。我测试的是一段简单的Socket程序。 
如果是codeblock问题可以往下跳转,跳过前奏: 
DEVC++报错:

C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x3f): undefined reference to `__imp_WSAStartup'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x6b): undefined reference to `__imp_socket'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0xa9): undefined reference to `__imp_htons'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0xdc): undefined reference to `__imp_bind'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x105): undefined reference to `__imp_closesocket'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x155): undefined reference to `__imp_recvfrom'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x182): undefined reference to `__imp_inet_ntoa'
C:\Users\persenal computer\Desktop\test\Server.o    Server.cpp:(.text+0x1f3): undefined reference to `__imp_sendto'
e:\dev c++\dev-cpp_gr\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe Server.o: bad reloc address 0x10 in section `.xdata'
C:\Users\persenal computer\Desktop\test\collect2.exe    [Error] ld returned 1 exit status
25      C:\Users\persenal computer\Desktop\test\Makefile.win    recipe for target 'Server.exe' failed
上述错误就是一堆未定义,一般这时dev会跳转到自动生成的Makefile.win文件里,并在这里

$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
显示报错信息,很明显是连接出了错误导致未成功生成.exe文件,并不是代码问题,也就是环境问题了,然后我通过更换编译g++版本、换成32位编译(说实话问题确实出在64和32这里了,但不知道为啥dev不认账非得按64位编译的来,也许没设置对)等都出不来,于是乎我就更换了codeclock尝试。 
发现了codeblock报以下错误:

||=== Client, Debug ===|
\Client\src\Client.cpp|8|warning: ignoring #pragma comment |
\Client\src\Client.cpp||In function 'int main(int, char**)':|
\Client\src\Client.cpp|14|error: 'WSASitartup' was not declared in this scope|
||=== Build finished: 1 errors, 1 warnings ===|
很相似的错误,确实就是环境问题了,我对codeblock比较熟悉,因此上述错误在我调试下可以通过 
针对10.05(比较老)的版本: 
Settings -> Compiler and debugger-> Global compiler settings-> Linker settings 
针对13.12版本: 
Settings -> Compiler settings -> Global compiler settings-> Linker settings 
注意我标注黑体部分路径你们肯定是要按照自己codeblocks的安装路径变换的,然后在Others linker Options里面添加:-lws2_32,重启codeblocks,测试一下应该就好了,如果还是不行,在刚刚的路径下的Link libraries里面点击Add按钮输入C:\ProgramFiles(x86)\CodeBlocks\MinGW\lib\libws2_32.a 
记住标黑体的要根据自己安装路径来更换。

既然找到真正问题所在,然后更换到dev上去更改(因为他们g++都是一样的),我们在:工具->编译选项(第一项) 打开后下面有个"在连接器命令行加入以下命令",打上勾,如果-static-libgcc这些的话,删掉,并且加上-lws2_32保存就可以了。

吐槽以下,dev这个编译器要不是改别人代码我真不会去用它,很多设置看不懂.........也许我还是太年轻

(30条消息) Dev C++的undefined reference to `__imp_htons'或codeclock的undefined reference to'WSAStartup to@8'相关编译器错_吉锋涛的博客-CSDN博客

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小任是个大神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值