0

How can I link more than one file(test) to an executable?

I have this code:

set(TEST_EXE_NAME Test)
add_executable(${TEST_EXE_NAME} t1.cc)
add_executable(${TEST_EXE_NAME} t2.cc)
add_test(Test ${TEST_EXE_NAME})

This of course doesn't compile and I understand why. But is there a way to put both tests into one executable?

  • I do this with command line arguments and combining the result to an overall result. That along with logging helps me identify the reason(s) for failure. – drescherjm Feb 10 '17 at 21:36
  • @drescherjm I'll try that! do you know if there's a way to do it without that? –  Feb 10 '17 at 21:37
  • `add_test` can parse your output using `PASS_REGULAR_EXPRESSION` or `FAIL_REGULAR_EXPRESSION`. That's more flexible but probably not what you wanted. – usr1234567 Feb 10 '17 at 21:44
  • @usr1234567 Thanks! Edited my question for an alternative I'd prefer. –  Feb 10 '17 at 22:05
  • 3
    Can you please give more details why you are trying to do this? Just a guess from my side: you want to have a "test runner" and I'm wondering if you know about [tag:ctest] coming with [tag:cmake]? It will run everything you have given with `add_test()`. So the simplest solution would be to build the two executables with different names and add both as tests. – Florian Feb 10 '17 at 22:38
  • @Florian thanks! I reset TEST_EXE_NAME after the first test. If you want to post this as an answer I'd be happy to accept it. –  Feb 13 '17 at 14:23

2 Answers2

1

You can give multiple source file names to the add_executable command:

add_executable(${TEST_EXE_NAME} t1.cc t2.cc)
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • I get the error that there are multiple definitions of main. –  Feb 13 '17 at 14:17
  • I guess I misunderstood your question. If you want to link multiple files into one executable, then my answer is for you. If you have multiple files that each must be linked into their own executable, follow the reply by anc. – Cris Luengo Feb 14 '17 at 15:43
0

Just repeat it.

set(TEST_EXE_NAME Test)
add_executable(${TEST_EXE_NAME} t1.cc)
add_test(Test ${TEST_EXE_NAME})
set(TEST_EXE_NAME Test1)
add_executable(${TEST_EXE_NAME} t2.cc)
add_test(Test1 ${TEST_EXE_NAME})

Since you're testing this way the names can be more descriptive for each executable anyway.