I am learning TDD, using GoogleTest framework. I have successfully built Gtest and have been able to build and run the samples. However, when I tried a simple sample I wrote, I am getting compilation errors.
Here is the source and the build commands I used:
// ################################################
//proj1.h
#ifndef __SCRATCH_PROJ1_H
#define __SCRATCH_PROJ1_H
int addOne(int i);
#endif /*__SCRATCH_PROJ1_H */
// ################################################
//proj1.cpp
#include "proj1.h"
int addOne(int i){
return i+1;
}
// ################################################
//proj1_unittest.cpp
#include "proj1.h"
#include "gtest/gtest.h"
// Test Function
TEST(addOneTest, Positive) {
EXPECT_EQ(1,addOneTest(0)); // <- Line # 24
EXPECT_EQ(2,addOneTest(1)); // <- Line # 25
EXPECT_EQ(40320, addOneTest(40319)); // <- Line # 26
}
TEST(addOneTest, Negative) {
EXPECT_FALSE(addOneTest(-1)); // <- Line # 30
}
GTEST_API_ int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Console output:
g++ -isystem ${GTEST_DIR}/include -pthread -c
/home/user1/scratch/proj1_unittest.cpp
/home/user1/scratch/proj1_unittest.cpp: In member function ‘virtual
void addOneTest_Positive_Test::TestBody()’:
/home/user1/scratch/proj1_unittest.cpp:24:5: error: ‘addOneTest’ was
not declared in this scope
/home/user1/scratch/proj1_unittest.cpp:25:5: error: ‘addOneTest’ was
not declared in this scope
/home/user1/scratch/proj1_unittest.cpp:26:5: error: ‘addOneTest’ was
not declared in this scope /home/user1/scratch/proj1_unittest.cpp: In
member function ‘virtual void addOneTest_Negative_Test::TestBody()’:
/home/user1/scratch/proj1_unittest.cpp:30:5: error: ‘addOneTest’ was
not declared in this scope
Judging from the line numbers in the error messages, it seems that the EXPECT_* macros have not been defined - BUT, I have included gtest/gtest.h in the compilation unit.
What is causing these errors - and how do I fix it?
As it says, addOneTest was not declared anywhere. I'm guessing you meant to call addOne instead.
Related
Im setting up the bsplib (https://github.com/Zefiros-Software/BSPLib) on a windows system (in VS Code) using WSL. When compiling I get the error message:
test.cpp:4:5: error: conflicting declaration of C function ‘int main()’
4 | int main()
| ^~~~
In file included from /mnt/d/study/software/bsp/include/bsp/bspExt.h:30,
from /mnt/d/study/software/bsp/include/bsp/bsp.h:34,
from test.cpp:2:
/mnt/d/study/software/bsp/include/bsp/bspClass.h:59:12: note: previous declaration ‘int main(int, char**)’
59 | extern int main(int argc, char **argv);
The program is used is just a bare example for BSP:
#include <iostream>
#include "bsp/bsp.h"
int main()
{
bsp_begin(bsp_nprocs());
int s = bsp_pid();
int p = bsp_nprocs();
printf("Hello World from processor %d / %d", s, p);
bsp_end();
return 0;
}
Compiled with:
g++ -I/mnt/d/study/software/bsp/include -g -lpthread -o main test.cpp
To my (quite limited) knowledge, the 'extern' in the header file should prevent the compiler from labelling the main as 'duplicate' of some sort. Im mostly interested in some of BSPs functionalities as part of a class of mine, that sadly does not include any support on the installation. What I've done so far:
Copied the include files from the repo
Added the include path to the compilation (-I Flag) and the -lpthread as instructed by the class script
Added the include path to the configuration (c_cpp_properties.json) [tested both with and without this, no difference]
Due to the many possible sources of that error (program, compiler, wsl, library, configuration, vs code, my stupidity) I cant determine where I am mistaken, nor am I able to find online resources to that combination.
I'm trying to get the yaml-cpp parser working on my computer. I followed the instructions on the README, which generated the file libyaml-cpp.a with no errors or warnings. Then I copied that file into a directory, let's call it /path/to/files, where I also put b.yaml, and main.cpp, which contains the following text:
// main.cpp
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
return 0;
}
This comes from the first line of the yaml-cpp tutorial. I tried compiling this while linking to the yaml-cpp library in a few different ways, all of which lead to the same compile-time error: use of undeclared identifier 'YAML'. Here are some of the things I tried:
g++ main.cpp -lyaml-cpp -L/path/to/files
g++ main.cpp libyaml-cpp.a
g++ main.cpp libyaml-cpp.a -lyaml-cpp -L/path/to/files
and so on. How do I compile this correctly or more properly debug this process?
==EDIT==
Now my main.cpp file looks like this:
// main.cpp
#include <iostream>
#include "yaml.h"
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
return 0;
}
Here's my compile command and error message:
$ g++ main.cpp -lyaml-cpp -I/Users/benlindsay/scratch/yaml-cpp/include -L/Users/benlindsay/scratch/yaml-cpp/build
main.cpp:10:3: error: use of undeclared identifier 'YAML'
YAML::Node config = YAML::LoadFile("b.yaml");
^
main.cpp:10:23: error: use of undeclared identifier 'YAML'
YAML::Node config = YAML::LoadFile("b.yaml");
^
2 errors generated.
make: *** [a.out] Error 1
/Users/benlindsay/scratch/yaml-cpp/include contains a yaml-cpp directory, which in turn contains all the .h files including yaml.h. /Users/benlindsay/scratch/yaml-cpp/build contains the lyaml-cpp.a file.
Ok, I downloaded yaml-cpp and tried out, Here is a working version
#include <iostream>
#include "yaml-cpp/yaml.h" //You need to prepend the yaml-cpp
int main(int argc, const char *argv[])
{
YAML::Node config = YAML::LoadFile("b.yaml");
//return 0; In cpp, return 0 is not required on main, hence commented
}
The compile using g++ -std=c++11 main.cpp -lyaml-cpp -I/Users/benlindsay/scratch/yaml-cpp/include -L/Users/benlindsay/scratch/yaml-cpp/build
I'm writing software to control a bladeRF radio card but I'm running into a strange compiler/linker error that I haven't been able to figure out. My code uses several functions and data structures defined in the library, libbladeRF, but for some reason I can't reference to one specific function.
However, if I modify the call with an improper argument type, g++ will throw an error to let me know that it doesn't conform to the definition, which seems to tell me that the linker is actually able to locate the reference.
What am I missing?
Initial error:
$ g++ bladeRF_test.cpp -o bladeRF_test -lbladeRF
/tmp/ccTWZzdJ.o: In function `enable_xb300()':
bladeRF_test.cpp:(.text+0x36a): undefined reference to `bladerf_xb300_set_amplifier_enable'
Code excerpt:
#include <iostream>
#include <string>
#include <libbladeRF.h>
using namespace std;
...
int set_xb300_pa(bool enable) {
bladerf_xb300_amplifier amp = BLADERF_XB300_AMP_PA;
if ( bladerf_xb300_set_amplifier_enable(dev, amp, enable) ) {
// Print error message
return -1;
} else {
// Print success message
return 0;
}
}
...
Function arguments changed from (dev, amp, enable) to (&dev, amp, enable):
$ g++ blade_hello.cpp -o blade_hello -lbladeRF
blade_hello.cpp: In function ‘int set_xb300_pa()’:
blade_hello.cpp:62:59: error: cannot convert ‘bladerf**’ to ‘bladerf*’ for argument ‘1’ to ‘int bladerf_xb300_set_amplifier_enable(bladerf*, bladerf_xb300_amplifier, bool)
^
In file included from blade_hello.cpp:4:0:
/usr/local/include/libbladeRF.h:2226:15: note: declared here
int CALL_CONV bladerf_xb300_set_amplifier_enable(struct bladerf *dev,
^
I am having a problem compiling my wxWidgets program.
#include <wx/wx.h>
#include <wx/url.h>
#include <wx/stream.h>
#include <wx/sstream.h>
int main(int argc, char* argv[])
{
wxURL url(wxT("http://google.com"));
if (url.GetError()==wxURL_NOERR)
{
wxString htmldata;
wxInputStream *in = url.GetInputStream();
if (in && in->IsOk())
{
wxStringOutputStream html_stream(&htmldata);
in->Read(html_stream);
}
wxPuts(html_stream.GetString();
}
}
When I try to compile it, I get the following errors:
main.cpp In function 'int main(int, char**)':
main.cpp 8 error: 'wxURL' was not declared in this scope
main.cpp 8 error: expected ';' before 'url'
main.cpp 9 error: 'url' was not declared in this scope
main.cpp 9 error: 'wxURL_NOERR' was not declared in this scope
main.cpp 12 error: 'wxInputStream' was not declared in this scope
main.cpp 12 error: 'in' was not declared in this scope
main.cpp 15 error: 'wxStringOutputStream' was not declared in this scope
main.cpp 15 error: expected ';' before 'html_stream'
main.cpp 16 error: 'html_stream' was not declared in this scope
main.cpp 18 error: 'html_stream' was not declared in this scope
=== Build finished: 10 errors, 0 warnings ===
What am I doing wrong? Should I be using OnInit() instead of int main(), even if I want the application to be a console, non-gui one?
Take a look into your setup.h file for wxWidgets and be sure that wxURL is #define as
#define wxUSE_URL 1
For more information into your setup file (setup.h), take a look at http://wiki.wxwidgets.org/Setup.H
To take a look at a sample of wxURL, take a look into the your wxWidgets folder, go to "samples" and look into "sockets_client".
There is also reference documents at
http://docs.wxwidgets.org/2.9.3/classwx_u_r_i.html
http://wiki.wxwidgets.org/WxURL
http://wiki.wxwidgets.org/Download_a_file_from_internet
This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited