Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

How can I compose translation units in C? [duplicate]

$
0
0

Essentially what I want to do is the C equivalent of:

translation_unit_list.cpp

std::vector<std::string> list;
bool addListItem(std::string str){
    list.push_back(str);
    return true;
}
int main(int argc, char **argv){
  for(std::string translation_unit_name : list){
    cout << translation_unit_name << endl;
  }
  return 0;
}

some_file.cpp

static bool initialized = addListItem("some file!");

some_file2.cpp

static bool initialized = addListItem("some other file!");

this way, translation_unit_list knows every translation unit that was compiled into the final executable. this allows the user to selectively compile what they want to be included. is there a way of achieving the same result with c?

Example desired behavior:

g++ -o exec1 translation_unit_list.cpp some_file.cpp 
./exec1
some file!

g++ -o exec1 translation_unit_list.cpp some_file2.cpp 
./exec1
some other file!

g++ -o exec1 translation_unit_list.cpp some_file.cpp some_file2.cpp 
./exec1
some file!
some other file!

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>