C++ 如何辨識代碼是在 gcc 上面跑還是 vc 上跑
有些時候我們會需要能夠識別到底是在 gcc 上還是 visual Studio 上跑,畢竟兩者之間相容性還是有很大區別的,可以利用 Macro 來偵測。
如以下範例,把代碼打在裡面就自然會在正確的編譯器上運行了。
#if defined(_MSC_VER)
/* VC */
#endif
#if defined(__GNUC__)
/* gcc */
#endif
本站建議搭配 Hover Zoom+ chrome 擴充套件,可自動放大圖片
( 站內文介紹: https://goo.gl/BnBSGa )
網站搜索不是很好用建議使用google底下括號內是範例,整串打進google搜索內;
[ site:https://charlottehong.blogspot.tw/ 你要搜索的內容 ]
#if defined(_MSC_VER)
/* VC */
#endif
#if defined(__GNUC__)
/* gcc */
#endif
關於 Visual Studio 2017 如何安裝可以參考 https://charlottehong.blogspot.com/2018/09/c-visual-studio-2017.html
建議直接解壓縮到C槽,本文範例即直接解壓縮到C
C:\opencv\build\x64\vc15\bin
LNK1104 無法開啟檔案 'opencv_worldXXXd.lib'
/**********************************************************
Name :
Date : 2016/05/29
By : CharlotteHonG
Final: 2016/05/29
**********************************************************/
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char const *argv[]) {
/* 畫布 */
Mat img(270, 720, CV_8UC3, Scalar(56, 50, 38));
/* 直線 */
line(img, Point(20, 40), Point(120, 140), Scalar(255, 0, 0), 3);
/* 實心方塊 */
rectangle(img, Point(150, 40), Point(250, 140), Scalar(0, 0, 255), -1);
/* 實心圓 */
circle(img, Point(330, 90), 50, Scalar(0, 255, 0), -1);
/* 空心橢圓 */
ellipse(img, Point(460, 90), Size(60, 40), 45, 0, 360, Scalar(255, 255, 0), 2);
/* 不規則圖形 */
Point points[1][5];
int x = 40, y = 540;
points[0][0] = Point(0 + y, 50 + x);
points[0][1] = Point(40 + y, 0 + x);
points[0][2] = Point(110 + y, 35 + x);
points[0][3] = Point(74 + y, 76 + x);
points[0][4] = Point(28 + y, 96 + x);
const Point* ppt[1] = { points[0] };
int npt[] = { 5 };
polylines(img, ppt, npt, 1, 1, Scalar(0, 255, 255), 3);
/* 繪出文字 */
putText(img, "Test Passed !!", Point(10, 230), 0, 3, Scalar(255, 170, 130), 3);
/* 開啟畫布 */
namedWindow("OpenCV Test By:Charlotte.HonG", WINDOW_AUTOSIZE);
imshow("OpenCV Test By:Charlotte.HonG", img);
waitKey(0);
return 0;
}
如果做到這邊才發現跟我一樣選錯了,要退回去include路徑那步重作
設定裡面 x86->x64 改了全部會歸零…
出現找不到 dll 檔案,重新啟動一下 Visual Studio 2017 。
#include <bits/stdc++.h>
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include
編譯器版本不一樣路徑會變,自己手動開進去找,大概就是2017還有14.10.25017這兩個會變吧。
注意要用另存,電腦預設是不能改副檔名的
/****************************************************************
Name : Test_Main.cpp
Date : 2016/11/07
By : CharlotteHonG
Final: 2016/11/07
*****************************************************************/
#include <iostream>
#include "Test.hpp"
using namespace std;
int main(int argc, char const *argv[]){
Test a(77);
a.pri();
return 0;
}
/*****************************************************************
Name : Test.hpp
Date : 2016/11/07
By : CharlotteHonG
Final: 2016/11/07
*****************************************************************/
#ifndef TEST_HPP
#define TEST_HPP
class Test{
public:
Test(int n);
void pri();
private:
int num;
};
#endif
/*****************************************************************
Name : Test.cpp
Date : 2016/11/07
By : CharlotteHonG
Final: 2016/11/07
*****************************************************************/
#include <iostream>
#include "Test.hpp"
using namespace std;
Test::Test(int n){
this->num=n;
}
void Test::pri(){
cout << "num=" << this->num << endl;
}
/********************************************
Name :
Date : 2016/11/07
By : CharlotteHonG
Final: 2016/11/07
*********************************************/
#include <iostream>
using namespace std;
// Test.hpp
class Test{
public:
Test(int n);
void pri();
private:
int num;
};
// Test.cpp
Test::Test(int n){
this->num=n;
}
void Test::pri(){
cout << "num=" << this->num << endl;
}
// Test_main.cpp
int main(int argc, char const *argv[]){
Test a(77);
a.pri();
return 0;
}
篇幅有點長拆兩篇講如何匯入現有檔案
參考:Visual Studio 使用方法 02