Cmake accuses multiple statements of a class

Asked

Viewed 67 times

0

Structure of the "project":

->shop
---->cpp store.
---->shop. h
---->main.cpp
---->Cmakelists.txt

When I try to compile Vscode accuses that there are multiple denifications of class members. Being that if I put an inline in the header file it compiles.

[build] Starting build [proc] Executing command: C: Tools Cmake bin cmake.EXE --build "c:/Users/Henrique/Documents/My Projects/Loja/build" -config Debug --target all -- -j 10 [build] Scanning dependencies of target app [build] [ 66%] Building CXX Object Cmakefiles/app.dir/loja.cpp.obj [build] [ 66%] Building CXX Object Cmakefiles/app.dir/main.cpp.obj [build] [100%] Linking CXX Executable app.exe [build] c:/mingw/bin/.. /lib/gcc/mingw32/8.2.0/.. /.. /.. /mingw32/bin/Ld.exe: Cmakefiles app.dir/Objects. a(loja.cpp.obj): in Function ZN4loja5helloERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE': [build] C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:13: multiple definition ofstore::hello(Std::__cxx11::basic_string, Std::allocator > const&)'; Cmakefiles app.dir/Objects. a(main.cpp.obj):C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:13: first defined here [build] c:/mingw/bin/.. /lib/gcc/mingw32/8.2.0/.. /.. /.. /mingw32/bin/Ld.exe: Cmakefiles app.dir/Objects. a(loja.cpp.obj): in Function ZlsRSoRK4loja': [build] C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:17: multiple definition ofOperator<<(Std::ostream&, store const&)'; Cmakefiles app.dir/Objects. a(main.cpp.obj):C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:17: first defined here [build] c:/mingw/bin/.. /lib/gcc/mingw32/8.2.0/.. /.. /.. /mingw32/bin/Ld.exe: Cmakefiles app.dir/Objects. a(loja.cpp.obj): in Function ZrsRSiR4loja': [build] C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:22: multiple definition ofOperator>>(Std::istream&, store&)'; Cmakefiles app.dir/Objects. a(main.cpp.obj):C:/Users/Henrique/Documents/My Projects/Loja/loja.cpp:22: first defined here [build] collect2.exe: error: Ld returned 1 Exit status [build] Cmakefiles app.dir build.make:99: Recipe for target 'app.exe' failed [build] mingw32-make.exe2: * [app.exe] Error 1 [build] Cmakefiles Makefile2:967: Recipe for target 'Cmakefiles/app.dir/all' failed [build] mingw32-make.exe1: * [Cmakefiles/app.dir/all] Error 2 [build] Makefile:115: Recipe for target 'all' failed [build] mingw32-make.exe: *** [all] Error 2 [build] Build finished with Exit code 2

Why does this happen? And how to solve.

2 answers

1


There are a lot of things wrong with this project.

Here’s a solution that works.
Can be (well) improved, but at least compiles and runs without errors.

Cmakelists.txt

cmake_minimum_required(VERSION 3.0.0)

project(LojaHeranca VERSION 0.1.0)

add_executable(app main.cpp loja.cpp)

shop. h

#ifndef LOJA_H
#define LOJA_H

#include <iostream>
#include <string>

// desaconselhavel colocar isso dentro de um include!!!
using namespace std;

class loja {

  private:
    string nome;

public:
    loja(string n = "Sem Nome") : nome(n) { cout << "Construindo..." << endl; }
    ~loja() { cout << "Destruindo..." << endl; };
    void hello(std::string const &name);
    friend ostream &operator<<(ostream &out, const loja &l);
    friend istream &operator>>(istream &in, loja &l);
};
#endif // LOJA_H

cpp store.

#include "loja.h"
#include <iostream>
#include <string>
/*
loja::loja(string n = "Sem Nome") {
    cout << "Construindo..." << endl;
}

loja::~loja() {
    cout << "Destruindo..." << endl;
}
*/
void loja::hello(std::string const &name) {
    std::cout << "Hello, " << name << "!\n";
}

ostream &operator<<(ostream &out, const loja &l) {
  out << l.nome << endl;
  return out;
}

istream &operator>>(istream &in, loja &l) {
  cout << "Enter the name: ";
  in >> l.nome;
  return in;
}

main.cpp

#include "loja.h"
#include <iostream>

using namespace std;

int main()
{
  loja f;
  f.hello("Teste");
}
  • I need a systematic way to add libs and linkalas to the main program. There is a linkage between libraries as well.

  • open another question with a project that has libs...this project that you have shown here does not have libs...and has a very strange thing: include de.cpp... it is very rare that this be necessary, and in this case here caused the errors of multiple definition

  • The question is: how to properly line the store lib. h with the main program.

  • 1

    its terminology is wrong: "store. h" is not a lib, is a include...lib would be a file. lib or . dll on Windows, and . so on Linux; cmake has several commands related to includes, but in this small project of yours it was not necessary to use these commands

  • Now it all makes sense. So I need to learn how linhar includes to my main program. That’s it?

  • I didn’t understand what you said, I don’t know what "linhar"

  • I meant link. Sorry.

Show 2 more comments

0

I found the solution in this link in it it is said that to avoid the problem just add a settings in the file of Cmakelists.txt:

set_source_files_properties(${SRCS} PROPERTIES HEADER_FILE_ONLY TRUE)

In case SRCS are all my implementation files of class members.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.