{"id":490,"date":"2020-01-16T11:01:09","date_gmt":"2020-01-16T03:01:09","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=490"},"modified":"2025-02-26T11:35:30","modified_gmt":"2025-02-26T03:35:30","slug":"relative_path_to_absolute_path","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2020\/01\/16\/relative_path_to_absolute_path\/","title":{"rendered":"\u76f8\u5bf9\u8def\u5f84\u8f6c\u7edd\u5bf9\u8def\u5f84"},"content":{"rendered":"<p>\u6574\u7406\u4ee3\u7801\u7684\u65f6\u5019\u9700\u8981\u628a\u7528\u5230\u7684\u6a21\u578b\u6587\u4ef6\u90fd\u653e\u5230\u5de5\u7a0b\u76ee\u5f55\u4e0b, \u4f46\u81ea\u5df1\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u8bfb\u53d6\u6a21\u578b\u7684\u51fd\u6570\u90fd\u9700\u8981\u7edd\u5bf9\u8def\u5f84, \u56e0\u6b64\u9700\u8981\u5148\u6709\u76f8\u5bf9\u8def\u5f84\u8f6c\u7edd\u5bf9\u8def\u5f84\u7684\u64cd\u4f5c.<\/p>\n<p><!--more--><\/p>\n<p><strong>\u53c2\u8003\u6750\u6599<\/strong><br \/>\n1. <a href=\"https:\/\/blog.csdn.net\/yaoqi_isee\/article\/details\/73310766\">C++\u8de8\u6587\u4ef6\u5171\u4eab\u5168\u5c40\u53d8\u91cf<\/a><br \/>\n2. <a href=\"https:\/\/blog.csdn.net\/lixungogogo\/article\/details\/52176911\">C\u8bed\u8a00\u4e0eC++\u4e2dstatic,extern\u7684\u7528\u6cd5\u53ca\u533a\u522b\u603b\u7ed3<\/a><\/p>\n<p>\u9996\u5148\u5229\u7528getcwd\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u5de5\u7a0b\u7684\u7edd\u5bf9\u8def\u5f84,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n#include &lt;direct.h&gt;\r\nchar *buffer;\r\n\/\/\u4e5f\u53ef\u4ee5\u5c06buffer\u4f5c\u4e3a\u8f93\u51fa\u53c2\u6570\r\nif ((buffer = getcwd(NULL, 0)) == NULL)\r\n{\r\n    perror(\"getcwd error\");\r\n}\r\nelse\r\n{\r\n    std::string input(buffer);\r\n    pathChange(input+\"\\\\..\\\\..\\\\\");\r\n    free(buffer);\r\n}\r\n<\/pre>\n<p>\u56e0\u4e3a\u81ea\u5df1\u6e05\u695amodel\u6587\u4ef6\u5939\u653e\u5728\u5916\u8fb9, \u6240\u6709\u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u7684\u4ee3\u7801\u542b\u6709&#8221;\\\\..\\\\..\\\\&#8221;, pathChange\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u7edd\u5bf9\u8def\u5f84\u7ecf&#8221;\\\\..\\\\..\\\\&#8221;\u5904\u7406\u540e\u7684\u7ed3\u679c,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\nvoid pathChange(string input) {\r\n    vector&lt;string&gt; inputpath, outputpath;\r\n    string tmp;\r\n    for (int i = 0; i &lt; input.size(); i++) {   \/\/\u5c06input\u5b57\u7b26\u4e32\u6309\u2018\/\u2019\u5206\u5f00\u5b58\u50a8\u5230inputpath \r\n\tif (input[i] != '\\\\') {\t\t\t\/\/\u5411\u91cf\u4e2d\u3002 \u6ce8\u610f\uff1a\u7b2c\u4e00\u4e2a\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff01 \r\n\t    tmp += input[i];\r\n\t}\r\n\telse {\r\n\t    inputpath.push_back(tmp);\r\n\t    tmp.clear();\r\n\t}\r\n    }\r\n\r\n    while (!inputpath.empty()) {\t\t\t\/\/\u5224\u65advector\u6700\u540e\u4e00\u4e2a\u6570\u636e\u662f\u5426\u4e3a\u201c..\u201d\uff0c\u82e5 \r\n\tif (inputpath.back() != \"..\") {\t\/\/\u662f\uff0c\u5219\u8fde\u7eed2\u6b21\u5220\u9664\u5c3e\u6570\u636e\uff0c\u82e5\u5426\uff0c\u5219\u5c06\u5c3e\u6570 \r\n\t    tmp = inputpath.back();\t\t\/\/\u636e\u5b58\u5230outputpath\u4e2d\uff0c\u5e76\u5220\u9664inputpath\u4e2d\u7684 \r\n\t    outputpath.push_back(tmp);\t\/\/\u5c3e\u6570\u636e\u3002 \r\n\t    inputpath.pop_back();\r\n\t}\r\n\telse {\r\n\t    int pop_num = 0;\r\n\t    int end = inputpath.size() - 1;\r\n\t    while (inputpath[end] == \"..\") {\r\n\t\tpop_num += 2;\r\n\t\t--end;\r\n\t    }\r\n\t    for (int i = 0; i &lt; pop_num; ++i) inputpath.pop_back();\r\n\t}\r\n    }\r\n\r\n    projectPath = \"\";\r\n    for (auto iter = outputpath.rbegin(); iter != outputpath.rend(); iter++) projectPath += (*iter) + \"\\\\\";\r\n}\r\n<\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f, \u4e00\u5f00\u59cb\u6211\u7684pathChange\u7684\u8fd4\u56de\u7c7b\u578b\u662fstring, \u7136\u9e45string\u5bf9\u8c61\u4e00\u822c\u4e0d\u80fd\u505a\u8fd4\u56de\u503c\u7684, \u5728\u8de8\u6a21\u5757\u8c03\u7528\u65f6\u5019\u4f1a\u51fa\u95ee\u9898, \u76f4\u63a5\u8fd4\u56destring\u5bf9\u8c61\u4e00\u822c\u4f1a\u662f\u7a7a, \u6c57\u2026\u2026 <strong>\u56e0\u4e3astring\u91cc\u9762\u7684\u6784\u9020\u51fd\u6570\u5728\u5806\u7a7a\u95f4\u52a8\u6001\u7533\u8bf7\u4e86\u5185\u5b58, \u4e0d\u5c5e\u4e8e\u6808\u53d8\u91cf, \u51fd\u6570\u7ed3\u675f\u65f6\u4f1a\u8c03\u7528string\u7684\u6784\u9020\u51fd\u6570, \u6790\u6784\u51fd\u6570\u91cc\u91ca\u653e\u4e86\u5728\u5806\u7a7a\u95f4\u7533\u8bf7\u7684\u5185\u5b58.<\/strong><\/p>\n<p>\u8fd8\u6709\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662fprojectPath\u662f\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf, \u8c8c\u4f3c\u662f\u7b2c\u4e00\u6b21\u4f7f\u7528extern\u5173\u952e\u5b57=\u3002= \u7a0d\u7a0d\u8bb0\u5f55\u4e00\u4e0bextern\u548cstatic\u7684\u7528\u6cd5.<\/p>\n<p>\u4e0b\u9762\u7684\u5c0f\u6817\u5b50\u6d89\u53ca\u5230\u4e09\u4e2a\u6587\u4ef6test.h, test.cpp, main.cpp.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\/\/ test.h\r\n#ifndef TEST_H\r\n#define TEST_H\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n\r\nextern int global_v;\r\n\r\nvoid modify();\r\n\r\nvoid print();\r\n\r\n#endif\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\/\/ test.cpp\r\n#include \"test.h\"\r\n\r\nusing namespace std;\r\n\r\nint global_v = 1;\r\n\r\nvoid modify()\r\n{\r\n    global_v++;\r\n    cout &lt;&lt; \"in test, value increases to \" &lt;&lt; global_v &lt;&lt; endl;\r\n}\r\n\r\nvoid print()\r\n{\r\n    cout &lt;&lt; \"in test, value is \" &lt;&lt; global_v &lt;&lt; endl;\r\n}\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\/\/ main.cpp\r\n#include \"test.h\"\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n    cout &lt;&lt; \"in main function, initial global value is \" &lt;&lt; global_v &lt;&lt; endl;\r\n\r\n    modify();\r\n    print();\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p><strong>static<\/strong><br \/>\n\u5f53\u6211\u4eec\u540c\u65f6\u7f16\u8bd1\u591a\u4e2a\u6587\u4ef6\u65f6, \u6240\u6709\u672a\u52a0static\u524d\u7f00\u7684\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u90fd\u5177\u6709\u5168\u5c40\u53ef\u89c1\u6027. static\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u548c\u53d8\u91cf\u7684\u524d\u7f00, \u5bf9\u4e8e\u51fd\u6570\u6765\u8bb2,static\u7684\u4f5c\u7528\u4ec5\u9650\u4e8e\u9690\u85cf. \u5229\u7528\u8fd9\u4e00\u7279\u6027\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u5b9a\u4e49\u540c\u540d\u51fd\u6570\u548c\u540c\u540d\u53d8\u91cf, \u800c\u4e0d\u5fc5\u62c5\u5fc3\u547d\u540d\u51b2\u7a81.<\/p>\n<p><strong>extern<\/strong><br \/>\nextern\u5219\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u8fd9\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u5728\u5176\u4ed6\u6587\u6863\u91cc\u5df2\u88ab\u5b9a\u4e49\u4e86. \u53e6\u5916\u7528\u6cd5\u662f\u5f53C\u548cC++\u6df7\u5408\u7f16\u7a0b\u65f6\u5047\u5982c++\u8c03\u7528\u7684\u662fc\u6e90\u6587\u6863\u5b9a\u4e49\u7684\u51fd\u6570\u6216\u53d8\u91cf, \u90a3\u4e48\u8981\u52a0extern\u6765\u544a\u8bc9\u7f16\u8bd1\u5668\u7528c\u65b9\u5f0f\u547d\u540d\u51fd\u6570:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\nextern \"C\"  \/\/\u5728c++\u6587\u6863\u91cc\u8c03\u7528c\u6587\u6863\u4e2d\u7684\u53d8\u91cf\r\n{\r\n   int j;\r\n   void callme();\r\n}\r\nint main()\r\n{\r\n   callme();\r\n}\r\n<\/pre>\n<p>\u56e0\u6b64, static\u7684\u8bbe\u8ba1\u6cd5\u5219\u662f:<br \/>\nA. \u82e5\u5168\u5c40\u53d8\u91cf\u4ec5\u5728\u5355\u4e2aC\u6587\u6863\u4e2d\u8bbf\u95ee, \u5219\u80fd\u591f\u5c06\u8fd9\u4e2a\u53d8\u91cf\u4fee\u6539\u4e3a\u9759\u6001\u5168\u5c40\u53d8\u91cf, \u4ee5\u964d\u4f4e\u6a21\u5757\u95f4\u7684\u8026\u5408\u5ea6;<br \/>\nB. \u82e5\u5168\u5c40\u53d8\u91cf\u4ec5\u7531\u5355\u4e2a\u51fd\u6570\u8bbf\u95ee, \u5219\u80fd\u591f\u5c06\u8fd9\u4e2a\u53d8\u91cf\u6539\u4e3a\u8be5\u51fd\u6570\u7684\u9759\u6001\u5c40\u90e8\u53d8\u91cf, \u4ee5\u964d\u4f4e\u6a21\u5757\u95f4\u7684\u8026\u5408\u5ea6;<br \/>\nC. \u8bbe\u8ba1\u548c\u4f7f\u7528\u8bbf\u95ee\u52a8\u6001\u5168\u5c40\u53d8\u91cf\u3001\u9759\u6001\u5168\u5c40\u53d8\u91cf\u3001\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u51fd\u6570\u65f6, \u9700\u8981\u8003\u8651\u91cd\u5165\u95ee\u9898.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u7406\u4ee3\u7801\u7684\u65f6\u5019\u9700\u8981\u628a\u7528\u5230\u7684\u6a21\u578b\u6587\u4ef6\u90fd\u653e\u5230\u5de5\u7a0b\u76ee\u5f55\u4e0b, \u4f46\u81ea\u5df1\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u8bfb\u53d6\u6a21\u578b\u7684\u51fd\u6570\u90fd\u9700\u8981\u7edd\u5bf9\u8def\u5f84, \u56e0\u6b64\u9700 &hellip; <a href=\"https:\/\/www.caiqinyi.cn\/index.php\/2020\/01\/16\/relative_path_to_absolute_path\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u76f8\u5bf9\u8def\u5f84\u8f6c\u7edd\u5bf9\u8def\u5f84<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/490"}],"collection":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":25,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":3652,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/490\/revisions\/3652"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}