【问题标题】:Visual Studio doesn't recognize GCC linker Errors in Makefile ProjectVisual Studio 无法识别 Makefile 项目中的 GCC 链接器错误
【发布时间】:2015-11-06 17:12:53
【问题描述】:

我们有一个交叉编译的 Visual Studio Makefile 项目。我们已经不得不引入solution similar to this 来让它识别编译器错误。 IE。我们引入了一个 Perl 脚本来解析 GCC 的输出并将其转换为 Visual Studio 可以理解的形式。如果我们声明:

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 实际上并不认为这是一个错误。有同样问题的 Visual Studio C++ 控制台程序有链接器错误:

1>  TestUndefinedReference.cpp
1>TestUndefinedReference.obj : error LNK2019: unresolved external symbol "int __cdecl something(int)" (?something@@YAHH@Z) 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'

但这不足以欺骗 Visual Studio 链接器错误解释器。 查看链接器错误的最低要求是什么?是否有任何解决方案可以在不直接解析文本的情况下工作?

【问题讨论】:

  • 在阅读您的代码项目链接后,总之这是一个好问题(请原谅我以前的怪癖)。您应该使这一点更加突出,并在简短的摘要中进行解释,以便为您的问题提供简明的答案。对不起,我自己没有。
  • 您是否尝试添加从“/”到“\”的斜线转换? $str =~ s/\//\\/;
  • @JimBlack 是的,我们确实转换了“\”。调整问题
  • @Mithaldu 你不能在 cmets 中有垂直空间,你可以使用 shift-enter 进行编辑,这可能会让你更容易。至于给出的示例,这只是为了显示 VS 链接器错误和 GCC 链接器错误之间的区别。 VS 是一个简单的例子,GCC 来自我正在尝试编译的实际项目。我实际尝试转换的后一个示例都来自同一个项目。
  • @Mithaldu 至于尝试,我们已经尝试了更多的转换。这就是为什么问题的关键是看到链接器错误的最低要求是什么?这很简单,定义明确并且在本网站范围内。我希望答案中有一个代码示例,但我没有在问题中发布它,因为我知道它是题外话。不过谢谢你的建议。

标签: visual-studio-2015


【解决方案1】:

根据the documentation...

输出的格式应该是:

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

地点:

  • {一个 | 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'
                                    ^

【讨论】:

  • 谢谢,我在构建 perl 脚本时没有找到这个文档。这突出了脚本的几个问题。
【解决方案2】:

所以看起来添加这一行就足够了:

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 语句生成的(以检测我们是否有 in 函数引用):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 2021-02-18
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2013-06-10
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多