Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. linker error lnk2005

linker error lnk2005

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiolinuxhelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roberto64_Ge
    wrote on last edited by
    #1

    I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards

    K L 2 Replies Last reply
    0
    • R Roberto64_Ge

      I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Roberto64_Ge wrote:

      main.cpp includes header.cpp which includes header.h

      This is probably the source of your problem. Visual Studio is probably compiling header.cpp and creating a separate object file, then compiles main.cpp, which includes header.cpp, and so creates two copies of the methods defined in header.cpp. In general it is a mistake to #include a cpp file in another cpp file. You main.cpp only needs to include header.h, which should only provide a declaration of your class. You would then compile main.cpp and header.cpp separately. You can do this on the ubuntu command line using

      g++ -Wall -Wextra main.cpp header.cpp -o main

      or alternatively

      g++ -Wall -Wextra -c main.cpp
      g++ -Wall -Wextra -c header.cpp
      g++ main.o header.o -o main

      In the above example, the -c option to g++ tells the compiler to only produce an intermediate object file (e.g. a .o file), and not try to produce an executable. The third line creates the final executable, and includes all the system libraries and startup code needed for the executable.

      Keep Calm and Carry On

      R 1 Reply Last reply
      0
      • R Roberto64_Ge

        I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok. I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj. I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Roberto64_Ge wrote:

        main.cpp includes header.cpp which includes header.h

        This means that all the definitions in header.cpp will be duplicated in the object files. Do not include .cpp files in others, only include the actual headers, i.e. in this case main.cpp should only include header.h.

        R 1 Reply Last reply
        0
        • K k5054

          Roberto64_Ge wrote:

          main.cpp includes header.cpp which includes header.h

          This is probably the source of your problem. Visual Studio is probably compiling header.cpp and creating a separate object file, then compiles main.cpp, which includes header.cpp, and so creates two copies of the methods defined in header.cpp. In general it is a mistake to #include a cpp file in another cpp file. You main.cpp only needs to include header.h, which should only provide a declaration of your class. You would then compile main.cpp and header.cpp separately. You can do this on the ubuntu command line using

          g++ -Wall -Wextra main.cpp header.cpp -o main

          or alternatively

          g++ -Wall -Wextra -c main.cpp
          g++ -Wall -Wextra -c header.cpp
          g++ main.o header.o -o main

          In the above example, the -c option to g++ tells the compiler to only produce an intermediate object file (e.g. a .o file), and not try to produce an executable. The third line creates the final executable, and includes all the system libraries and startup code needed for the executable.

          Keep Calm and Carry On

          R Offline
          R Offline
          Roberto64_Ge
          wrote on last edited by
          #4

          Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?

          K R 2 Replies Last reply
          0
          • R Roberto64_Ge

            Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #5

            If you've created a VS C++ project, it knows how to deal with files that you add to the project. All you should need to do for a simple project is hit Build, and the IDE will take care of the rest.

            Keep Calm and Carry On

            1 Reply Last reply
            0
            • R Roberto64_Ge

              Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019) 1) a create a new cc++ emty project 2) Add main.cpp , header.cpp and header.h to the project. 3) Include header.h in main.cpp Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that? And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?

              R Offline
              R Offline
              Roberto64_Ge
              wrote on last edited by
              #6

              Ok I tried and I did it. I right clicked on header.cpp and it gave the option to compile. This created the header.obj. Then I right clicked on the main.cpp and again I had the option to compile it and this generated the exe. Before doing all this I included in the main.cpp only header.h, Now it works. Thanks for the clear explanation

              K 1 Reply Last reply
              0
              • L Lost User

                Roberto64_Ge wrote:

                main.cpp includes header.cpp which includes header.h

                This means that all the definitions in header.cpp will be duplicated in the object files. Do not include .cpp files in others, only include the actual headers, i.e. in this case main.cpp should only include header.h.

                R Offline
                R Offline
                Roberto64_Ge
                wrote on last edited by
                #7

                ok thanks

                1 Reply Last reply
                0
                • R Roberto64_Ge

                  Ok I tried and I did it. I right clicked on header.cpp and it gave the option to compile. This created the header.obj. Then I right clicked on the main.cpp and again I had the option to compile it and this generated the exe. Before doing all this I included in the main.cpp only header.h, Now it works. Thanks for the clear explanation

                  K Offline
                  K Offline
                  k5054
                  wrote on last edited by
                  #8

                  That's one way to do it, but not the most efficient: [Building and Cleaning Projects and Solutions - Visual Studio (Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/ide/building-and-cleaning-projects-and-solutions-in-visual-studio?view=vs-2022)

                  Keep Calm and Carry On

                  R 1 Reply Last reply
                  0
                  • K k5054

                    That's one way to do it, but not the most efficient: [Building and Cleaning Projects and Solutions - Visual Studio (Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/ide/building-and-cleaning-projects-and-solutions-in-visual-studio?view=vs-2022)

                    Keep Calm and Carry On

                    R Offline
                    R Offline
                    Roberto64_Ge
                    wrote on last edited by
                    #9

                    ok i will read it :thumbsup::thumbsup::thumbsup: :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups