2015-11-06 79 views
5

我們有一個交叉編譯的visual studio Makefile項目。我們已經必須引入一個solution similar to this以使它識別編譯器錯誤。 IE瀏覽器。我們引入了一個Perl腳本來解析GCC的輸出,並將其轉換爲Visual Studio可以理解的形式。如果我們聲明:Visual Studio無法識別Makefile項目中的GCC鏈接器錯誤

int succ = thisRandomFunction(userPointer, 1, 1); 

沒有定義thisRandomFunction那麼我們將得到鏈接器錯誤:

1> ./program.a(taskqueue.o): In function `TaskQueueAdd': 1> 
D:\Git\program\taskqueue.c(94,1) : undefined reference to `thisRandomFunction' 1> 
collect2: ld returned 1 exit status 1> make: *** [program.exe] Error 1 

但視覺工作室實際上並不承認這是一個錯誤。同樣的問題在Visual Studio的C++控制檯程序有鏈接錯誤:

1> TestUndefinedReference.cpp 
1>TestUndefinedReference.obj : error LNK2019: unresolved external symbol "int __cdecl something(int)" ([email protected]@[email protected]) referenced in function _main 
1>D:\Projects\New folder\TestUndefinedReference\Debug\TestUndefinedReference.exe : fatal error LNK1120: 1 unresolved externals 

通過使用該轉換器:

sub parseLinkerError 
{ 
    my $str = $_[0]; 
    my $find = "undefined reference to"; 
    my $replace = "error LNK2019: unresolved external symbol"; 
    $str =~ s/$find/$replace/g; 
    return $str 
} 

我們可以將此轉換:

1> d:\Git\program/taskqueue.c:94: undefined reference to `thisRandomFunction' 

這個

1> D:/Git/eV+/program/taskqueue.c(94,1) error LNK2019: unresolved external symbol `thisRandomFunction' 

B這不足以欺騙Visual Studio鏈接器錯誤解釋器。 它看到鏈接器錯誤的最低要求是什麼?有沒有解決方案可以直接解析文本?

+0

總而言之,你的樣本失敗的閱讀你的鏈接代碼項目後,一個很好的問題(請原諒我以前的怪癖)。你應該讓這個更加突出,在一個簡短的總結中解釋,爲你的問題得到一個簡潔的答案。我自己沒有一個,對不起。 –

+0

您是否嘗試添加從'/'到'\'的斜槓轉換? $ str =〜s/\ // \\ /; –

+0

@JimBlack是的,我們轉換「\」。調整問題 –

回答

1

根據the documentation ...

輸出的格式應該是:

{filename (line# [, column#]) | toolname} : 
[any text] {error | warning} code####: localizable string 

其中:

  • {A | b}是a或b的選擇。
  • [ccc]是一個可選的字符串或參數。

例如:

C:\sourcefile.cpp(134) : error C2143: syntax error : missing ';' before '}' 
LINK : fatal error LNK1104: cannot open file 'somelib.lib' 


因爲它缺少必要的冒號

D:/Git/eV+/program/taskqueue.c(94,1) error LNK2019: unresolved external symbol `thisRandomFunction' 
            ^
+0

謝謝,我在構建perl腳本時沒有找到這個文檔。這突出了劇本的幾個問題。 –

0

所以好像加入這一行,就足以做到這一點:

taskqueue.obj : error LNK2019: unresolved external symbol "thisRandomFunction" referenced in function TaskQueueAdd 

至於我可以告訴將通過以下鍵,可以檢測到錯誤:

[*].obj : error LNK2019: unresolved external symbol "[*]" referenced in function [*] 

[*]可以是任何東西或什麼也不是(它甚至不需要是實際的功能)。

所以下面的腳本就足以解析它:

my $previousUsedInFunction = ""; 

sub parseLinkerError 
{ 
    my $str = $_[0]; 
    my $find = "undefined reference to"; 

    #check that our string contains the undefined reference to substring. 
    if (index($str, $find) != -1) { 

     #Use regular expressions to get the important filename. 
     my $filename = ""; 
     if ($str =~ /\\([a-zA-Z0-9_.]*)\(/) { 
      $filename = $1; 
      #replace whatever filename we find with the .obj equivelent. 
      $filename =~ s/\.cpp$/\.obj/g; 
      $filename =~ s/\.h$/\.obj/g; 
      $filename =~ s/\.c$/\.obj/g; 
     } 

     #Use regular expressions to find the function name. 
     my $function = ""; 
     if ($str =~ /\`([a-zA-Z0-9_.()]*)\'/) { 
      $function = $1; 
     } 

     #create the final string similar to what visual studio expects. 
     my $finalStr = " " . $filename . ' : error LNK2019: unresolved external symbol "' . $function . '" referenced in function ' . $previousUsedInFunction . "\n"; 

     return $finalStr; 

    } 
    return $str; 
} 

凡串$previousUsedInFunction使用下面的if語句(來檢測,如果我們有一個函數引用)主解析循環產生:

elsif (/\.o\): In function \`([a-zA-Z0-9_.():]*)\'/) { 
    print ("$_"); 
    $previousUsedInFunction = $1;  
}