在Ubuntu下,你可以使用以下代码来获取MAC地址:
#include <iostream>using namespace std;#include <iostream>
#include <fstream>
#include <string>std::string getMACAddress()
{std::string macAddress;std::ifstream file("/sys/class/net/ens33/address");//your ethernet may be eth0if (file.is_open()){std::getline(file, macAddress);file.close();}return macAddress;
}int main()
{std::string macAddress = getMACAddress();std::cout << "MAC Address: " << macAddress << std::endl;return 0;
}
注意,这个代码假设你的网络接口是eth0。如果你的网络接口不是eth0,你需要相应地更改文件路径。此外,你需要在编译时链接libstdc++库,你可以使用以下命令编译代码:
```bash
g++ -std=c++11 -o get_mac_address get_mac_address.cpp
```
然后运行可执行文件:
```bash
./get_mac_address
```
或者直接用QtCreator创建C++项目。