How to make char array avaliable in multiple .cpp files?

落花浮王杯 提交于 2019-12-12 03:59:29

问题


I have a working program which must be split into multiple parts, for edition purposes. In this program is needed to keep user login info in char arrays to be able to connect to SQL, and this connection info is used many times in parts of the program that will end up in separated .cpp files, which will compile in a single program.

The problem is that if they are declared in just one file, they will be missing in the rest, and if they are declared in all of them, there will be duplicated definitions.

So, to make a concrete and simple example, if I have the following code:

#include <mysql++/mysql++.h>
#include <iostream>

using namespace std;
using namespace mysqlpp;

char server[]   =   "localhost";
char user[]     =   "root";
char pass[]     =   "xxxxxxx";
char db[]       =   "prog";

void function()
    {
    Connection con;
    con.connect("", server, user, pass);
    con.select_db(db);
    //action1...;
    }

void separated_function()
    {
    Connection con;
    con.connect("", server, user, pass);
    con.select_db(db);
    //action2...;
    }

int
main( int argc, char *argv[] )
{
    cout << "INICIO\n";

    function();
    separated_function();
    //something else with the mentioned variables...;

    cout << "FIN\n";
    return 0;
}

How can it be split correctly, to have function(), another_function() and main() in separated .cpp files,and make server, user, pass and db avaliable to all of them.

I know there must be many ways, but any working one is good enough, since I'm not getting any results so far.

Thanks for any help.

NOTE: This question is not about how to use the variables with MySQL, but how to split the program correctly.


回答1:


You want to use extern in the seperated source files, or in a common header that is included in the seperated source files. You will define them in one (and only one) cpp file. Here is an example:

main.h

void function();
void seperated_function();

namespace myGlobals {
  extern char server[];
  extern char user[];
  extern char pass[];
  extern char db[];
}

main.cpp

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

namespace myGlobals {
  char server[] = "localhost";
  char user[] = "root";
  char pass[] = "xxxxxxx";
  char db[] = "prog";
}

int main(int argc, char *argv[]) {
  std::cout << "main.cpp\n";
  std::cout << myGlobals::server << "\n";
  std::cout << myGlobals::user << "\n";
  std::cout << myGlobals::pass << "\n";
  std::cout << myGlobals::db << "\n\n";
  function();
  seperated_function();
  return 0;
}

function.cpp

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

void function() {
  std::cout << "function.cpp\n";
  std::cout << myGlobals::server << "\n";
  std::cout << myGlobals::user << "\n";
  std::cout << myGlobals::pass << "\n";
  std::cout << myGlobals::db << "\n\n";
}

seperated_function.cpp

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

void seperated_function() {
  std::cout << "seperated function.cpp\n";
  std::cout << myGlobals::server << "\n";
  std::cout << myGlobals::user << "\n";
  std::cout << myGlobals::pass << "\n";
  std::cout << myGlobals::db << "\n\n";
}

The namespace myGlobals is not required, but if I am going to use global variables I at least like to put them in their own namespace.



来源:https://stackoverflow.com/questions/32700265/how-to-make-char-array-avaliable-in-multiple-cpp-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!