Cannot compile file using catch2 CATCH_CONFIG_MAIN - c++

I've started out learning how to use Catch2 for testing my C++ code and am trying to set up a simple test.
My folder structure consists of three files all in the same folder:
catch.cpp //this is the catch_amalgamated.cpp file from GitHub
catch.hpp //this is the catch_amalgamated.hpp file from GitHub
test.cpp //this is my test file
All I have written in test.cpp is:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
When I try to compile test.cpp I get the following error which I think indicates that there is no main() function found(?):
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
When I add a nominal main() function, the file compiles successfully, but as I understand it #define CATCH_CONFIG_MAIN is supposed to create the main() function for you so something clearly isn't working.
Can anyone shed any light on this?

The error was occurring because the default branch that google takes you to is the development branch of catch2, so I was using version 3 files and following version 2 documentation (as it has not been updated yet). Once I downloaded the v.2 file everything started working fine.

Related

Compiling multiple files in Visual Studio Code resulting in error

I'm relatively new to coding in C++ and have started working with main and header files and I've created a program to test it out, however, the following program results in the following compiler error:
Undefined symbols for architecture x86_64:
"Print()", referenced from:
_main in test-7d0225.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is my current code.
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Print();
return 0;
}
test.h
#ifndef TEST_H
#define TEST_H
void Print();
#endif
test1.cpp
#include <iostream>
#include "test.h"
using namespace std;
void Print() {
cout << "Hello" << endl;
}
Nothing I've found online has helped me and my only assumptions are that my compiler isn't set up correctly. I've tried compiling both of these of files by typing in "g++ test.cpp test1.cpp", as well, but yields similar results. I would like to note that I am on Mac as well. Please feel free to leave any comments or suggestions for how I've asked this question, this is my first time on stack overflow.
Your code seems just fine, I’ll will try to compile it on vscode myself.
One problem could be your vscode launch.json. I have had the same problem as what I am describing. Vscode can be quite “Finicky” with low level languages like c and c++. In your launch file (or tasks file if it calls something in tasks) make sure you compile *.cpp files in the folder. I would look something like {folder}/**.cpp. To find the exact command look on vscode’s official site.
Given that you tried compiling your code outside of vscode and assuming that everything is in the same folder, it might be a coding problem. Again, your code looks fine. Go online and find some learning to code website with multiple file coding. Copy their code and do the same thing that you did with your code. If it doesn’t work there is definitely a problem with how to code is being compiled. Otherwise revise your code and compare to find the problem.
Also, could you provide the terminal command vscode prints (I am also on Mac). It will be in the terminal tab in the g++ section, not in bash)
P.S. I am also new to stack overflow so any suggestions you have for me will be great.

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

C++ - no rule to create target SaltPepper.o

I know there are several questions like this, no answer I have found so far seems to solve my problem.
I am using eclipse.
I coded something in the main function, and it worked fine.
I then an external function with the code in the main and now I get funny mistakes.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <random>
#include <ctime>
#include <cstdlib>
using namespace std;
using namespace cv;
void createSaltandPepper();
int main(int argc, char** argv) {
createSaltandPepper();
return 0;
}
No, as I am trying not to give to extensive an example again, here is the outside of the function:
void createSaltandPepper() {
//mycode
}
At least it does not seem to be a spelling mistake.
However, the error is:
make all
make: *** No rule to make target 'SaltPepper.o', needed by 'Display'.
I do wonder why it is trying to make a tagert called SaltPepper.o if my function is called createSaltandPepper.
Can somebody help me?
Edit: I did not conciously create a makefile, as I said I am working with eclipse.
And again, the code works fine as long as it is inside the main function instead of inside createSaltandPepper().
My file is called DisplayImage.cpp, the code above is in this file, including the function createSaltandPepper();
The only two function in this file are the main function and createSaltandPepper();
There are no other source files in the project.
I am sorry if I come across as rather stupid: I am a Java programmer and have not a clue about makefiles and such.
See this screenshot:
I now created a new project and added my old file into it, this time calling it Display.cpp
"build all" resulted in an error in subdir.mk which reads:
subdir.mk:18: recipe for target 'Display.o' failed
make: *** [Display.o] Error 1
The complete Error message for the build is as follows:
make all
Building file: ../Display.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0-std=c++11 -MMD -MP -MF"Display.d" -MT"Display.d" -o"Display.o" "../Display.cpp"
subdir.mk:18: recipe for target 'Display.o' failed
g++: error: argument to ‘-fmessage-length=’ should be a non-negative integer
make: *** [Display.o] Error 1
Update
I switched to Netbeans.
It now works.
First, this error has nothing to do with compilers, this error message is produced by the build system (make in your case). Basically, eclipse seems to call make, which figures out what are dependencies between files and which of them need recompiling and in turn calls the compiler to compile them. Every C++ source file is then compiled to an object file .o (typically with the same name: SaltPepper.cpp -> SaltPepper.o). Then all the object files are linked together with the libraries to form the final executable.
This particular error message tells you that your executable is specified to depend on the object file SaltPepper.o, but the build system does not know how to produce it. Most likely there is no corresponding SaltPepper.cpp file. So check if such file exists. If not, check the settings of your project (or the Makefile if you created it manually) and see if all the cpp filenames are specified correctly. You should add all the cpp files with your code to the project, and remove all the extraneous cpp files.
Update: Note that build system operates on the file level. It does not know anything about functions etc., only source files. Maybe this is the source of the confusion.

C++ Permission Denied Error

I'm fairly new to C++, I've been coding in Java for a few years. About a week ago I tried getting the Boost library to work with Codeblocks, and have run into error after error after error. I've managed to fix most of them but this one is driving me up a wall. My code returns two errors when compiled:
ld.exe||cannot find C:\boost_1_60_0\stage\lib: Permission denied|
||error: ld returned 1 exit status|
I simply cannot figure out how to fix this, I've been searching for help online for days. From what I've been able to figure out, the permission denied error is due to (as the error suggests) lack of permission to access the directory, but none of the fixes I've found online have worked.
Here is my code, although I don't think the code is related to the error.
#include "complex.h"
#include <cmath>
using namespace csis3700;
#define BOOST_TEST_MODULE ComplexTests
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
const double tol = 0.01;
BOOST_AUTO_TEST_CASE(zero_arg_constructor_should_not_crash) {
complex c;
}
The complex class is just a class that simulates complex numbers, right now it is just an empty constructor.
Build settings:
Your search directories does not include that folder. Double check that your search directories tab is populated with "C:\boost_1_60_0\stage\lib" or if you are using an environment variable, that it is set and linked properly here.
Additionally the error specifically is because you are looking for boost.lib as a file and not as a directory. Removing this from your current linker settings and moving it under the search directories->linker tab should resolve the error.
You might have missed "return 0;" in the main file.

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.