2

C ++我如何运行makefile输出

下面是我的MakeFile,我想问一下如何运行我的unitTest.cpp,因为当我用NetBean MakeFile 时,使用下面的MakeFile,main.exe 实际上是main.cpp 输出

但我想运行 unitTest.cpp 的输出

我如何运行 unitTest.cpp

# ExampleTests Project

SRCS = main.cpp currencyConverter.cpp unitTest.cpp

HDRS = currencyConverter.h unitTest.h

PROJ = main



# Remaining lines shouldn't need changing

# Here's what they do:

#   - rebuild if any header file or this Makefile changes

#   - include CppUnit as dynamic library

#   - search /opt/local for MacPorts

#   - generate .exe files for Windows

#   - add -enable-auto-import flag for Cygwin only



CC = g++

OBJS = $(SRCS:.cpp=.o)

APP = $(PROJ).exe

CFLAGS = -c -g -Wall -I/opt/local/include

ifeq (,$(findstring CYGWIN,$(shell uname)))

  LDFLAGS = -L/opt/local/lib

else

  LDFLAGS = -L/opt/local/lib -enable-auto-import

endif

LIBS = -lcppunit -ldl



all: $(APP)



$(APP): $(OBJS)

    $(CC) $(LDFLAGS) $(OBJS) -o $(APP) $(LIBS)



%.o: %.cpp $(HDRS)

    $(CC) $(CFLAGS) $< -o $@



clean:

    rm -f *.o $(APP)

下面是我的 unitTest.cpp

#include "unitTest.h"
#include "currencyConverter.h"

CPPUNIT_TEST_SUITE_REGISTRATION(unitTest);


unitTest::unitTest() {
}

unitTest::~unitTest() {
}

void unitTest::setUp() {
}

void unitTest::tearDown() {
}

void stringToUpper(string&);

void unitTest::testStringLowerToUpper()
{
string str = "ILOVECPLUSPLUS";
string str2 = "IloveCplusplus";

cout << "\nChecking if string 1 '" << str << "' equals string 2 '" << str2 << "'";
CPPUNIT_ASSERT_EQUAL(str,str2);

//this part i will use my stringToUpperFunction to test.
currencyConverter c;

c.stringToUpper(str2);

cout << "\nChecking if string 1 '" << str << "' equals string 2 '" << str2 << "'";
CPPUNIT_ASSERT_EQUAL(str,str2);

}
4

1 回答 1

1

将另一个目标(例如testrunner.exe)添加到您要测试的 .cpp 文件 + 您的 testsuite .cpp 文件 + 另一个构成您的 testrunner 应用程序的 main() 的 .cpp 文件到您的 make 文件中。有了这个,您可以添加另一个目标测试,这取决于testrunner.exe只调用 testrunner.exe 可执行文件。

于 2012-07-30T18:52:01.330 回答