How to compile project, that uses gtest with gcc compiler? - gcc

I have simple source code, that includes gtest and launches testing. Here are the file tests.c:
#include <gtest/gtest.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(" init GTest ");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I launched build using compiler gcc:
gcc -o MY_UNIT_TESTS tests.c -I/usr/include /usr/lib/libgtest.a /usr/lib/libgtest_main.a -lpthread
And it is failed with this error:
In file included from tests.c:1:0:
/usr/include/gtest/gtest.h:54:18: fatal error: limits: No such file or directory
compilation terminated.
If I use compiler g++, everything is ok. I need to test library, that was built using gcc compiler, so my test should be also build with gcc.
How can I build this code using gcc compiler ?

Google Test is written in C++ (and your are tests too) and you need to use g++ to compile your tests. You can build your library with gcc and you can call your C code from your C++ tests, but there are some things to be aware of.

Related

Include error when trying to include <wuapi.h>

Recently I tried to create tool using Windows Update Agent API in C++.
The problem is, even include of the wuapi.h header file causes problems on my machine.
It keeps saying, the header file could not be found.
#include <wuapi.h>
int main(int argc, char **args)
{
return 0;
}
Attempt to compile the simple code ends up predictably:
gcc -o tool.exe file.cpp -pedantic -Wall -Wextra
file.cpp:1:10: fatal error: wuapi.h: No such file or directory
#include <wuapi.h>
^~~~~~~~~
compilation terminated.
I found almost no information related to this issue on the Internet so far. That means I'm not sure what's wrong at all.
I'm using Windows 10.0.18362.592 but more importantly I'm using mingw-w64 8.1.0 as a compiler.
At this point I'm not sure, whether mingw-w64 supports this part of Win32 API. I found no useful information though.

How to fix undefined reference to `_imp__pthread_create'

I use MinGW on windows7 32bit.
And I can’t compile my source which uses pthread.
My code is below.
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Error happens as I compile it.
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
C:\Users\xxx\AppData\Local\Temp\cc9OVt5b.o:trylpthread.c:(.text+0x25): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status
I use following pthread library.
pthreads-w32-2.8.0-3-mingw32-dev
And here is libpthread.dll.a in /usr/local/lib
Does anyone know how to fix this problem?
The commandline:
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
does not make sense.
-L <dir> is a linker option that directs the linker to search for required libraries
in directory <dir>. Thus you are telling the linker to search for required libraries in
path /usr/local/lib/libpthread.dll.a, which is not a directory, while on the other hand
you are not telling the linker to link any libraries at all. That is why it fails to find any
definition for _imp__pthread_create.
Neither does the program you have posted make sense. The lines:
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
say:-
If I have not defined a preprocessor macro pthread_create then compile:
return ((int*)(&pthread_create))[argc];
else compile:
(void)argc;
return 0;
Well if you had defined a preprocessor macro pthread_create, e.g.
#define pthread_create whatever
then the code you would compile would be:
(void)argc;
return 0;
And since you have indeed not defined any such macro, the code you compile is:
return ((int*)(&pthread_create))[argc];
which fails at linkage, as you see. And if that code was compiled with pthread_create so defined,
it would be:
return ((int*)(&whatever))[argc];
Rewrite your program as:
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
return ((int*)(&pthread_create))[argc];
}
Compile with:
gcc -Wall -I /usr/local/include -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o /usr/local/lib/libpthread.dll.a
Remember that when you get the program compiled and linked, the appropriate pthreadGC??.dll
must be found at runtime in one of the places where the program loader searches for dlls.
Better still, uninstall your MinGW and your pthreads-w32-2.8.0-3-mingw32-dev and
install a more up-to-date Windows port of GCC, e.g. TDM-GCC (simplest) or mingw-w64. Pick the 32-bit version, if your Windows system
is 32-bit. These toolchains come with built in pthread support, as GCC standardly does.
Compile with:
gcc -Wall -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o -pthread
(not -lpthread)

How do you compile C++ programs that include LLVM API headers?

I'm trying to use the C++ compiler to compile the following program:
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
llvm::errs() << "Expected an argument - IR file name\n";
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::SMDiagnostic err;
llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );
if( !mod )
{
err.print( argv[0], errs() );
return 1;
}
return 0;
}
I'm trying to compile the program using the following command:
clang++ main.cpp -o main
However, when I compile, I'm getting the following compile error:
main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
^
1 error generated.
In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.
Any help would be greatly appreciated.
You can use the following command:
g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main
Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.
Thank You
You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5
You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout
Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.

mingw32 linker error when including QDebug

I have this minimal example:
QT -= gui
CONFIG += qt console
SOURCES += main.cpp
#include <QDebug>
int main(int argc, char** argv)
{
return 0;
}
which gives this link error when building the project:
c:/qtsdk/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: final link failed: Invalid argument
The link command looks like this:
g++ -Wl -Wl -Wl,-subsystem,console -mthreads -o debug\test.exe debug/main.o -L"c:\QtSDK\Desktop\Qt\4.8.1\mingw\lib" -lQtCored4
My setup:
Windows XP SP3
Qt SDK version 1.2.1 (QtCreator 2.4.1, Qt Desktop version 4.8.1) (fresh install at C:\QtSDK\)
MinGW32 version 4.4.0 (included in Qt SDK at C:\QtSDK\mingw\)
If I remove the #include <QDebug>, it compiles fine. If I include some other Qt header file, like for example QCoreApplication, it compiles fine, too.
EDIT: Here is a very strange minimal example. Consider an empty main function like above. Now if i put these includes, it fails to link:
#include <QWidget>
#include <QVariant>
But if I remove one of them, it links without an error.
What's the problem? Why doesn't mingw tell me what the invalid argument is?
Im wondering is the linker could not find the lQtCored4 lib? Is it actually in the -L directory?

openmp on dev c++

Is there anyway to use openmp with dev c++. I have seen links on how to use in Visual Studio, but i am more comfortable with Dev C++ interface.
Adding /openmp in the linker command line doesnt work either.
I couldnt find the library to download too. Am i missing something.
I tried running this sample code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
#pragma omp parallel
{
printf("Hello, world.\n");
}
return 0;
}
From where I read it was mentioned Output on a computer with 2 Cores and 2 threads will be hello world printed twice. I have a core i7 but it was printed only once.
Tools > Compiler Options > Check the option "Add the following commands when compiler is called" > in the text area put "-fopenmp"
Compile and execute again :)
I do not know Dev C++, but to enable openmp you also need to add the flag -fopenmp to your compiler.
Additional to linking to omp.
With g++ it look like this
g++ yourProgram.cpp -o yourProgram -lgomp -fopenmp
-fopenmp will tell the compiler to generate parallel code. I hope this will help.
You have to include -fopenmp in
Project-> Project Option->Parameters - Link
and
Tools ->Compiler Options (General)
(check "add the following commands when calling the compiler"
and include -fopenmp in the textBox
I have also included #include <omp.h>
dev-c++ version 5.6.1
there is only the parallel region , the processor is informed that there is what parallelize , but as is parallelize the code they have to say via builders, probably what you want to use : #pragma omp sections
I guess you also have to include the header file #include < omp.h > separately

Resources