0

I'm very new to C++ and could use some help. I'm trying to link a file my_help_fxns.cpp to my main.cpp file so i can use those functions in main.cpp, but when i try linking I get the following error for each function in my_help_fxns:

C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here

So it says I'm defining twice, but I don't know how to get around this. I have a class called data_vars_class. i include my_help_fxns at the top of data_vars_class.cpp, and use the helper fxns successfully in methods for that class. An instance of the class is created at the top of main.cpp. however if i try to use the helper functions in main() in main.cpp, without declaring "my_help_fxns.cpp" at the top of main.cpp, it says functions arent found, and if i do declare it at the top of main.cpp, i get the duplication error its been declared twice. How can I fix this, thanks!

this is the structure of my project

main.cpp ==>

#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.cpp"   <--- including here gives duplication error, but if i dont, its functions not found error

DataVars dataVars;

int main () {

    my_help_fxns::pause_program();

    return 0; 
}

data_vars_class.hpp ==>

#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>

class DataVars
{
    private:
         ...
    public:
         ...
}

data_vars_class.cpp ==>

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>

#include "data_vars_class.hpp"
#include "my_help_fxns.cpp"

...i can use my_help_fxns here with no problem, as an instance of this class is created before main() in main.cpp

my_help_fxns.cpp ==>

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

namespace my_help_fxns
{
    void pause_program() {
        std::string dummy;
        std::cout << "Enter to continue..." << std::endl;
        std::getline(std::cin, dummy);            
    }
}

And here is the build command for the file in Geany:

g++ main.cpp data_vars_class.cpp -o a.out

Thanks for your help!

4
  • The way you've minimized your code isn't good. We would never know where the compiler is exactly conflicting since there's no function defined anywhere. Commented Jul 3, 2020 at 4:18
  • Also, you can't use a .cpp extension to implement the code insider it as a header file (given in data_vars_class.cpp). Commented Jul 3, 2020 at 4:20
  • i added the "pause_program" function I'm trying to call in my_help_fxns.cpp. I'm not sure what you mean by "consider it as a header", and how would I implement that? thanks Commented Jul 3, 2020 at 4:21
  • Do not do #include with .cpp file Commented Jul 3, 2020 at 4:40

2 Answers 2

1

Don't include the my_help_fxns.cpp into the other CPP files since that will effectively define those functions in all the CPP files. This violates the one definition rule.

Instead

  • create a header file that declares (but not defines) those functions
  • include that header file in all the CPP files
  • add my_help_fxns.cpp to the compilation command line
Sign up to request clarification or add additional context in comments.

Comments

0

Make changes to your files as described:

main.cpp

#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.hpp" // change file extension from cpp -> hpp

DataVars dataVars;

int main () {

    my_help_fxns::pause_program();

    return 0; 
}

data_vars_class

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>

#include "data_vars_class.hpp"
// #include "my_help_fxns.cpp" --> Not required here

And then you may simply run:

g++ -o a.out main.cpp; ./a.out

Gives here:

Enter to continue...
sdfsdfsdfsd    // --- INPUT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.