I'm having a problem with a project that was originally a C project and is being extended with C . E.g. all the APIs are C, and the application layer is C . It mostly works, except I seem to be unable to define constructors that do any sort of processing in pre-main. It's like the memory exists, and the reference is valid, but the constructor itself just doesn't execute. Running -Wall and -Wextra, the only error I get is that I'm missing a memory region definition for .ctors.
I did notice that the linker flags include -nostartfiles, so I tried disabling that flag but I ended up with some additional memory region warnings about the size of .ctors and .dtors changing and an undefined reference to main
, which I thought was... odd.
So, any idea where I would start investigating the bug? I'm thinking maybe GCC is putting constructors in __main
but the linker script is pointing the start of the program to something other than main()
?
SSCCE below:
#include <iostream>
using namespace std;
class Foo {
public:
Foo() {
bar_ = 10;
};
Foo(int bar) : bar_(bar) {};
void setBar(int val) {
bar_ = val;
}
int getBar() {
return bar_;
}
private:
int bar_;
};
Foo myObj;
Foo otherObj(10);
int main(){
// Default Constructor
cout << myObj.getBar() << endl; // Prints 0
myObj.setBar(10);
cout << myObj.getBar() << endl; // Prints 10
// Parameterized constructor
cout << otherObj.getBar() << endl; // Prints 0
otherObj.setBar(10);
cout << otherObj.getBar() << endl; // Prints 10
// Parameterized constructor (local)
Foo thirdObj(10);
cout << thirdObj.getBar() << endl; // Prints 10
}
Subreddit
Post Details
- Posted
- 6 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...