cannot resolve LNK2005 multiply defined symbols

Bryan Kelly 321 Reputation points
2022-11-21T01:49:29.647+00:00

Windows 11, Visual Studio 2019, C++
I started getting this error:

1>ripemd_160_test_data.obj : error LNK2005: "char (* ripemd_input)[90]" (?ripemd_input@@3PAY0FK@DA) already defined in ripemd_160_13.obj

1>E:\WX\ripemd_160_13\Debug\ripemd_160_13.exe : fatal error LNK1169: one or more multiply defined symbols found

I have removed almost all the code with only one instance of that item and still get the error. Here is the main project file:

include <iostream>

include "ripemd_160_test_data.h"

int main()
{
std::cout << "Hello World!\n";
}

Yeah, that it. Here is the utility include file.

#pragma once  
#ifndef RIPEMD_160_TEST_DATA_H  
#define RIPEMD_160_TEST_DATA_H  

#include <stdint.h>  
#include <string>  

const size_t rm_count = 8;  
const size_t max_in = 90;  

char ripemd_input[rm_count ][ max_in ] =  
{  
   { "a" }  
// deleted stuff  
};  

#endif  

Please note the #pragma and the #define.
And here is the cpp file.

// test data for RIPEMD-160 test data  
// A RIPEMD-160 output is always the same length.  

#include "ripemd_160_test_data.h"  

Yeah, it down to just that.

I have done a clean and build and continue to get that same error. There is nothing left to remove.
What am I doing wrong.

Edit: I did use the "101010" tool to mark the code as such and it did not work.
Edit: And I started a new console project and added just those two files to the project, nothing else. The error is still there.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,540 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 10,036 Reputation points Microsoft Vendor
    2022-11-21T03:04:45.567+00:00

    Hi, @Bryan Kelly

    The problem is that global variable is being accessed by multiple files.

    Modified snippet:
    ripemd_160_test_data.h

    extern char ripemd_input[rm_count][max_in];   
    

    ripemd_160_test_data.cpp

    #include "ripemd_160_test_data.h"  
    
    char ripemd_input[rm_count ][ max_in ] =  
    {  
    { "a" }  
    // deleted stuff  
    };  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bryan Kelly 321 Reputation points
    2022-11-21T03:20:31.83+00:00

    Did you look at the "#pragma once"
    Did you look at the "#ifndef ..."
    Both of those commands to the compiler were designed to prevent it from creating the item multiple times.
    I started using that stuff back in the 1980s and 1990s.

    If it is declared "external" in the h file, then it is "external" to the h file, and every file that incorporates the h file sees it declared external, and there is nothing left to actually create that entity.


  2. Bryan Kelly 321 Reputation points
    2022-11-21T06:03:43.317+00:00

    I stepped away for a while and thought about this deeply. Your point(s) are getting through to me.
    Thank you for your time and patience.

    0 comments No comments