C++ template aliasing g++ 5.4 - c++11

I'am not able to compile any trivial C++ aliasing declaration.
Here is my dev env :
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Here is my snippet (file tstFrameProd.cpp):
template<typename T = double>
struct mystruct {};
template<typename T = double> using myalias = mystruct<T>;
int main(int argc, char *argv[])
{
return 0;
}
Here is my compilation process :
g++ -g -std=c++11 -W -Wall -I../include -I../../../Toolbox/CShmRingBuf/ -I/opt/matrox_imaging/mil/include -Woverloaded-virtual -ansi -pipe -fno-for-scope -DGNU_GCC -DDEBUG -c -o tstFrameProd.o tstFrameProd.cpp
Here is my compilation error message :
tstFrameProd.cpp:20:31: error: expected unqualified-id before ‘using’
template<typename T = double> using myalias = mystruct<T>;

You're compiling with -ansi which negates the effect of -std=c++11. Simply remove it.

Related

Undefined reference to sequence when trying to input a vector

This seems a simple but somehow the compile sends this error message which I'm not able understand thus correct my code.
This is a simplified version of what I did, just so it can appear the error for you:
Main.cpp
include "myfunction.h"
int main(){
std::vector<int> myVet = {1,4,3};
sequence(1,2,1,myVet);
}
myfunction.h
#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end,
int step, std::vector<int> skip);
myfunction.cpp
#include "myfunction.h"
void sequence(int start, int end,
int step, std::vector<int> skip){
auto x = 0;
};
This gives me an error message which says
In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
Could you please explain me why it appears?
This is the following command which I've been using for compiling
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp -o main.out
You are only passing main.cpp to g++.
g++ needs to know about myfunction.cpp where your function is defined so as to compile and link it to your program.
The command to use should be:
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out

what is the gcc option for add header file in complie time?

How to compile this code?
/* main.c file*/
#include <stdio.h>
int main(void)
{
int c;
c = add(5,5);
printf("sum is %d\n",c);
return 0;
}
Add.c(in same directory)
/*add.c*/
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
Add.h(in same directory)
/* add.h file*/
int add(int, int);
Then i am create a object file for add.c
$ gcc -c -Wall add.c -I.
Then i am try to create a object file for main.c
$ gcc -c -Wall main.c -I.
main.c:6:2: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
c = add(5, 5);
^
Please any one tell, how to give the headfile name in compiling time.
you can either use #include "Add.h" at the top of your main.c (this is the preferred route), or you can use the -include command line flag (this is discouraged because it does not scale and is not portable):
gcc -c -Wall main.c -I. -include Add.h

How to use shared library

These are my C codes simply print “Hello" Message. And I want to make mylib.c as shared library.
[mylib.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
int mylib(){
printf("### Hello I am mylib #####\n");
return 0;
}
[drive.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
At the firest I compiled mylib.c with folowing command line to make mylib.o
gcc –fPIC –g –c –Wall mylib.c
Then tried to make it shared librarly like this
gcc -shared -Wl,-soname,libmylib.so.1 -o /opt/lib/libmylib.so.1.0.1 mylib.o -lc
And I did ldconfig to update /etc/ld.so.cache
Finaly I compiled drive.c link with mylib but linker showed error
gcc -g -Wall -Wextra -pedantic -I./ -L./ -o drive drive.c –lmylib
/usr/bin/ld: cannot find –lmylib
Dose someone tell me how can I compile it?
In my way, you have to follow some ways to use shared library in C.
At first I have created a header file named "shared_library.h", in this file I have introduced a function named "method" as a function of this library.
The code is following:
/*-------This is starting of shared_library.h file-----------*/
void method();
/*-------------This is ending of shared_library.h file--------*/
Then I have defined the method in another file named "shared_library.c". The definition as in code is:
/*-------------This is starting of shared_library.c file---------*/
#include "shared_library.h"
void method()
{
printf("Method is called");
}
/*-------------This is ending of shared_library.c file---------*/
And finally, the header "shared_library.h" is ready to use. I use the library in my main C file named "main.c". The contents of "main.c" are as follows:
/*-------------This is starting of main.c file----------------*/
#include <stdio.h>
#include "shared_library.h"
int main()
{
method();
return 0;
}
/*-------------This is ending of main.c file----------------\*/
I found this article ld cannot find an existing library.
It works if I change to gcc -g -Wall -Wextra -pedantic -I./ -L/opt/lib -o drive drive.c –l:libmylib.so.1

Not able to use srand48() after changing to c++ 11

Why am I not able to compile my code to c++ 11 and use the srand48 function?
I have a program where I play around with some matrices.
The problem is that when I compile the code with the -std=c++0x flag.
I want to use some c++11 only functions and this is my approach to do so.
It compiles without any problems if I do not specify the c++ version. Like this:
g++ -O2 -Wall test.cpp -o test -g
Please correct me if I have misunderstood what the mentioned flag does.
I run my code on a Windows 7 64-bit machine and compile through cygwin. I use g++ version 4.5.3 (GCC). Please comment if more information is required.
For some unknown reason (even to myself) then all my code is written in one compilation unit.
If the error is caused by a structural error then you should also feel free to point it out. :)
I receive the following errors:
g++ -std=c++0x -O2 -Wall test.cpp -o test -g
test.cpp: In function ‘void gen_mat(T*, size_t)’:
test.cpp:28:16: error: there are no arguments to ‘srand48’ that depend on a template parameter, so a declaration of ‘srand48’ must be available
test.cpp:28:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test.cpp:33:28: error: there are no arguments to ‘drand48’ that depend on a template parameter, so a declaration of ‘drand48’ must be available
Here is a sub of my code, it generates the errors shown above.
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <limits.h>
#include <math.h>
#define RANGE(S) (S)
// Precision for checking identity.
#define PRECISION 1e-10
using namespace std;
template <typename T>
void gen_mat(T *a, size_t dim)
{
srand48(dim);
for(size_t i = 0; i < dim; ++i)
{
for(size_t j = 0; j < dim; ++j)
{
T z = (drand48() - 0.5)*RANGE(dim);
a[i*dim+j] = (z < 10*PRECISION && z > -10*PRECISION) ? 0.0 : z;
}
}
}
int main(int argc, char *argv[])
{
}
Regards Kim.
This is the solution that solved the problem for me:
First n.m. explained that srand() can not be used when compiling with -std=c++0x.
The correct flag to use is -std=gnu++11 however it require g++ version 4.7+
Therefore, the solution for me was to compile my code with -std=gnu++0x
The compile command = g++ -O2 -Wall test.cpp -o test -g -std=gnu++0x
If you explicitly set -stc=c++03 you will get the same error. This is because drand48 and friends are not actually a part of any C++ standard. gcc includes these functions as an extension, and disables them if standard behaviour is requested.
The default standard mode of g++ is actually -std=gnu++03. You may want to use -std=gnu++11 instead of -std=c++0x, or pass -U__STRICT_ANSI__ to the compiler.

Link Object File GCC/G++

I have my Main C++ Class main.cpp...
#include "fs.h"
int main(void)
{
return minit();
}
The fs.h:
#ifndef __FS__
#define __FS__
int minit (void);
#endif
And a fs.o (with minit() into) file that is already an object file, compiled with g++ without -g.
Here is my makefile:
myfs: main.o fs.o
g++ -o myfs -m32 -Wall fs.o main.o
main.o: main.cpp fs.h
g++ -o main.o -m32 main.cpp
Every time I try to link everything, the linker says that in main.cpp there is a undefined reference to minit(); What could it be?
You have defined a function called minit() within actually IMPLEMENTING it - that is why you are having this problem.
You need to actually write the function minit():
int minit(void) {
return 0;
}
For example...
You should point it to compiler that the function is defined elsewhere. Try changing this declaration:
int minit (void);
to this
extern int minit (void);

Resources