2

I've got a C++ project in XCode that builds a static library. I'd like to add a main method to the project to allow me to test some of the code in the library.

In an attempt to do this, I first duplicated the project's target, and I then added a main method to this second target (as per this answer). When I build and run this new target, however, my main method doesn't execute.

Does anyone have any idea what I'm doing wrong?

Community
  • 1
  • 1
dB'
  • 7,838
  • 15
  • 58
  • 101
  • this makes no sense. do you want to add a method to a static library? or do you want to test the library? – thang Feb 14 '13 at 17:42
  • I want to test part of the library. That's the only reason I'd be adding a main method. Is this is a crazy idea? Should I look into using some kind of test framework or something instead? (I'm new to C++ and XCode.) – dB' Feb 14 '13 at 17:45
  • 1
    why don't you just create an app that links to your library? you can add main to the library, but it still won't be executable.. it's just a library. now you see why it doesn't make sense? adding main to the library doesn't let you test it, so the two things you're trying to do are totally independent. – thang Feb 14 '13 at 17:46
  • 1
    I get it. Thanks! When I work in Java, I sometimes add a (temporary) main method to a class as a quick, informal way of testing its behaviour. I guess that's not done in C++. I appreciate the help. – dB' Feb 14 '13 at 17:50
  • 1
    @dB' I have the same problem. I want to use a small piece of code to quickly get some insight of a vast library. In java you can add a main and debug line by line and quickly make sense of a lot of things. The answer below is not really helpful for such case. – Sherry Sep 19 '18 at 20:58

1 Answers1

5

The typical way to test a library is that you write a separate piece of code that exercises the libary, and compares the result with expected results.

In this case, the main will go into the test program, not the library. Having a main in the library is not the right thing, and will lead to very curious problems for the user of the library if the user forgets to supply a main...

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227