I want to compile a C code in ubuntu using mex which is configured with gcc. I can smoothly compile the code in OSX. However, when I want to compile it in Linux, the compiler generates the error on the comment lines starting with // (it works fine with /* */. Since the program includes several header files from third-party libraries, I cannot substitute // with /* */.
I would like to know whether there is any way around to overcome this problem.
MATLAB version: R2012b
gcc version in Linux: 4.7.2
gcc version in OSX: 4.2.1
Any help is appreciated
Edit:
Here is the command I use to compile the code:
mex -g -largeArrayDims -ldl TDSVDHNGateway.c
Here is the error generated by mex:
In file included from TDSVDHNGateway.c:2:0:
TDS.h:17:1: error: expected identifier or ‘(’ before ‘/’ token
TDS.h:26:2: error: unknown type name ‘index_t’
TDS.h:27:2: error: unknown type name ‘index_t’
In file included from TDSVDHNGateway.c:2:0:
TDS.h:146:3: error: unknown type name ‘index_t’
TDSVDHNGateway.c:37:3: error: unknown type name ‘index_t’
TDSVDHNGateway.c: In function ‘mexFunction’:
TDSVDHNGateway.c:166:25: error: ‘index_t’ undeclared (first use in this function)
TDSVDHNGateway.c:166:25: note: each undeclared identifier is reported only once for each function it appears in
Line 17 in header file is:
//Defining index_t
typedef size_t index_t;
If I replace //Defining index_t with /*Defining index_t*/ the code will be compiled with no problem.
From the gcc docs;
In GNU C, you may use C++ style comments, which start with ‘//’ and continue until the end of the line. Many other C implementations allow such comments, and they are included in the 1999 C standard. However, C++ style comments are not recognized if you specify an -std option specifying a version of ISO C before C99, or -ansi (equivalent to -std=c90).
Under Linux, by default mex adds -ansi, which disables C++ comments. Either update your mexopts file, replacing -ansi with -std=c99 or run mex with;
mex -g -largeArrayDims -ldl CFLAGS="\$CFLAGS -std=c99" TDSVDHNGateway.c
Related
I am compiling small program with couple of header files and a source file. the code is here
https://elixir.bootlin.com/linux/v5.10/source/tools/testing/selftests/net/psock_tpacket.c
https://elixir.bootlin.com/linux/v5.10/source/tools/testing/selftests/kselftest.h
https://elixir.bootlin.com/linux/v5.10/source/tools/testing/selftests/net/psock_lib.h
It needs kernel header files so I tried something like this
# gcc -I//usr/src/linux-headers-5.10.0-kali7-common/include psock_lib.h kselftest.h psock_tpacket.c -o t.o -I.
But AM getting compilation errors like this
psock_lib.h:50:24: error: array type has incomplete element type ‘struct sock_filter’
1 ⨯
there are number of these errors.
above error simply simply thrown because struct sock_filter not found which exists in linux headers
in my usr/src/ directory
I have linux-headers-5.10.0-kali7-common & linux-headers-5.10.0-kali7-amd64
in gcc command I am specifying -I/usr/src/linux-headers-5.10.0-kali7-common/include is this correct?
both linux-headers-5.10.0-kali7-common/include and linux-headers-5.10.0-kali7-amd64/include have makefile
I'm getting started with SDL and C programming. I have experience with other programming languages, but linking/compiling libraries in C is new to me. I am using Mac 10.8 and have installed latest stable 2.0 using the instructions in the read me (./configure; make; make install). Here is the sample code that I am trying to compile:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
int main(void)
{
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
return 0;
}
When I try to compile my script using gcc example.c, I get an error:
example.c:3:17: error: SDL.h: No such file or directory
example.c: In function ‘main’:
example.c:7: error: ‘SDL_INIT_VIDEO’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: ‘SDL_INIT_TIMER’ undeclared (first use in this function)
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
example.c:11: error: ‘SDL_Quit’ undeclared (first use in this function)
I tried searching the wiki, and tutorials, and any kind of documentation that I could find, but I could not find any example anywhere that showed how to properly compile a C program that uses SDL.
What do I need to do to compile this program?
A general hint for C beginners: read error logs top-down: often fixing first error will resolve all other. In your case first error is:
example.c:3:17: error: SDL.h: No such file or directory
As others have said, you need to instruct gcc where to find SDL.h. You can do this by providing -I option.
To check where SDL.h is installed by default I would issue
./configure --help
in the directory where you did build libsdl. Then look for --prefix, under Linux default prefix is often /usr/local. To compile your example I would issue (on Linux):
gcc example.c -I/usr/local/include
But the above command compiles and links the code. After successful compilation, gcc would throw another bunch of errors, one of them being undefined reference.
To prevent that, full command line to build your example (on Linux at least) would be:
gcc example.c -I/usr/local/include -L/usr/local/lib -lSDL
Where:
-I points compiler to directory with SDL.h,
-L points linker to directory with libSDL.a (or libSDL.so),
-l instructs linker to link with library, in our case libSDL.a or libSDL.so. Note that the lib prefix and .a/.so suffix is missing.
Please note that I didn't check this instruction, even on Linux machine (on the other hand I have no access to Mac OS machine).
One more thing: by default binary with the compiled and linked example will be called a.out. To change that you can provide -o option to gcc.
I found out you can use a tool called pkg-config to find out the compiler flags expected for a specific library.
$ pkg-config --cflags --libs sdl2
-D_THREAD_SAFE -I/usr/local/include/SDL2 -I/usr/X11R6/include -L/usr/local/lib -lSDL2
$ gcc example.c $(pkg-config --cflags --libs sdl2)
If you are using a Makefile, you need to prefix the command with shell:
all:
gcc example.c $(shell pkg-config --cflags --libs sdl2)
tl;dr
sudo apt install libsdl1.2-dev
You are missing the SDL library files. Just instal them and everything should work out of the box.
Different versions ans extensions
There are multiple versions of SDL, make sure you install the one that is required by your application. There are also additional libraries (called projects) for SDL that you also need to install if you use their features. For example if you use TTF fonts or image related functionalities. Just press TAB twice after you typed in sudo apt install libsdl to see all the available packages.
I'm trying to compile a C project with Eclipse, under Ubuntu 12.10 using GCC.
When I use these flags:
http://i.stack.imgur.com/FTxkG.jpg
http://i.stack.imgur.com/MVA2k.jpg
eclipse fails to compile.
The compilation error is the following:
error: ISO C forbids an empty translation unit [-pedantic]
make: *** [.metadata/.plugins/org.eclipse.cdt.make.core/specs.o] Error 1
Why do these problems appear,
and how should I compile with eclipse with the checked flags?
When trying to compile manually from the Terminal with these flags, it compiles successfully. thanks!
Your C project has an empty source file.
You can either:
1) Remove that file. If it is empty why do you need it?
2) Be more relax with your compilation flags, with -pedantic instead of -pedantic-errors for example
3) Add some dumb code to make gcc believe the file is not empty, for example add #include <stdio.h>.
I just started using llvm and trying to print basic commands of llvm, when i tried to execute
clang -cc1 ~/hello.c -ast-print
getting following error
fatal error: 'stdio.h' file not found
#include<stdio.h>
^
int main()
{
printf("hi\n");
return 0;
}
1 error generated.
so please help me out.
Thanks!
With -cc1 you invoke the frontend, not the gcc-compatible compiler driver. The latter knows how to find standard header includes, the former doesn't.
If you want to use -cc1 with files including from standard libraries, then either supply all relevant paths with -I, or just run it on preprocessed files.
I have successfully compiled a C-program by GCC on Mac with GD2 library installed directly from sources. Now I am trying to do that with GD2 library installed via MacPorts and have the following error message:
plotgeometry.c:5:16: error: gd.h: No such file or directory
plotgeometry.c: In function 'PlotGeometry':
plotgeometry.c:28: error: 'gdImagePtr' undeclared (first use in this function)
plotgeometry.c:28: error: (Each undeclared identifier is reported only once
plotgeometry.c:28: error: for each function it appears in.)
plotgeometry.c:28: error: expected ';' before 'im'
plotgeometry.c:29: warning: ISO C90 forbids mixed declarations and code
plotgeometry.c:748: error: 'im' undeclared (first use in this function)
plotgeometry.c:748: warning: implicit declaration of function 'gdImageCreate'
plotgeometry.c:752: warning: implicit declaration of function 'gdImageColorAllocate'
plotgeometry.c:780: warning: implicit declaration of function 'gdImageSetPixel'
plotgeometry.c:801: warning: implicit declaration of function 'gdImagePng'
plotgeometry.c:809: warning: implicit declaration of function 'gdImageDestroy'
I guess, I need to provide the path to GD2 library for GCC. The gd.h is found in the following dirs
$ find /opt/local/ -name 'gd.h'
/opt/local//include/gd.h
/opt/local//var/macports/software/gd2/2.0.35_7/opt/local/include/gd.h
I have added /opt/local/include to my $PATH variable, but it did't help. Do I need to path an additional parameter with that path to GCC?
Could you help me with this?
You don't use $PATH. Add it via the -I command-line option to gcc. For direct builds:
gcc -I/opt/local/include ...
For make:
CPPFLAGS='-I/opt/local/include' make
You'll also need to reference the library while linking. Use -L/opt/local/lib -lgd or, via make:
CPPFLAGS='-I/opt/local/include' LDFLAGS='-L/opt/local/lib' LDLIBS='-lgd' make
You can, of course, set these variables in your Makefile.
You need to add -I/opt/local/include to compiler arguments (and not $PATH which is only used by shell to find executables).