Link time info gathering
-
Hello! I wonder if there is some way or some trick to gather some info during compile time and using it at runtime ? example1: We have many cpp files. We have some #define CPP_COUNTER 0. There would be some code (macro?) at the begining of every cpp file which would increase this counter for 1. When linking the project, CPP_COUNTER would hold the number of cpp files. In runtime I would be aware of cpp files in the project by reading CPP_COUNTER definition. example2: We have many files in the project, but functionaly they are divided in few modules (dsp, writter, reader, etc). At compile time every cpp file would have at the begining some code which would register (to some global variable) to which module it belongs. Something like REGISTER_MODULE(_FILENAME_, "disk writter") In runtime I would be aware of all the modules in the system by looking at the global variable of all registered modules. I would need something like example2 shows. Is it even possible? Is there some way? Best regards, Rostfrei
-
Hello! I wonder if there is some way or some trick to gather some info during compile time and using it at runtime ? example1: We have many cpp files. We have some #define CPP_COUNTER 0. There would be some code (macro?) at the begining of every cpp file which would increase this counter for 1. When linking the project, CPP_COUNTER would hold the number of cpp files. In runtime I would be aware of cpp files in the project by reading CPP_COUNTER definition. example2: We have many files in the project, but functionaly they are divided in few modules (dsp, writter, reader, etc). At compile time every cpp file would have at the begining some code which would register (to some global variable) to which module it belongs. Something like REGISTER_MODULE(_FILENAME_, "disk writter") In runtime I would be aware of all the modules in the system by looking at the global variable of all registered modules. I would need something like example2 shows. Is it even possible? Is there some way? Best regards, Rostfrei
Hi Rostfrei, One way of doing this is to create a dummy counting class and just create instances of it. Hence this becomes:
countcpp.h:
#ifndef __COUNT_H__
#define __COUNT_H__#include <iostream.h>
#define REGISTER_MODULE(file,des) static CCountCpp g_cppFile (file, des);
class CCountCpp
{
public:
CCountCpp (void) { }CCountCpp (const char *strFile, const char *strDes)
{
cout << strFile << "{ " << strDes << " }" << endl;
++m_iCppCount;
}static int m_iCppCount;
};#endif // __COUNT_H__
This just creates a static CCountCpp object per REGISTER_MODULE define. Because of the static objects they can be the same name across modules as long as you are not including .cpp files. To use:
#include "countcpp.h"
REGISTER_MODULE(__FILE__, "disk writer");
int CCountCpp::m_iCppCount = 0;
void main (void)
{
CCountCpp c;
cout << "Cpp count: " << c.m_iCppCount << endl;
}Naturally you would remove the main, it is just there for show. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook