自己遇到問題清單

C/C++ 部分

1. pragma once 跟 ifndef 的區別

2. 變量或者函數在連接的時候被多處定義的問題

描述:如有文件head.hpp, test.cpp, main.cpp


//File haed.hpp
#include<string>
void foo()
{
}
std::string testStr="abc";
const std::string constStr="js";
void test();
#endif

//File test.cpp
#include"head.hpp"

void test()
{
}

//File main.cpp
#include"head.hpp"
int main()
{
}


問題是分別編譯test.cpp跟main.cpp生產.o目標文件沒問題,鏈接test.o main.o生成最終的可執行文件時候foo函數在多處定義, string testStr也在多處定義,爲啥const的constStr沒有問題呢? 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章