Is it possible for gcc to name mangle? - gcc

I had some code that wasn't working until I added extern "C" before the name of a function, even though I was compiling using gcc. The file's name did, however, end in ".cpp".
Is it possible for gcc to name mangle? Did it intelligently pick up the file extension ".cpp"?

The gcc compiler driver looks at the file extension. If it is .cpp (or .cc, .C and a few more), the file is compiled as a C++ file:
Compiling C++ Programs

Related

Can we use .lib file using GCC compiler

I have a library file with .lib extension. I want to use this file in my project which uses GCC compiler to build.
Is it possible to use the .lib file?? I tried but I got this error:
"File Format not recognized."

Assembler used by golang when building with and without cgo

Let's say I have a golang package, which contains some assembly code:
demopkg/
source1.go
source2.go
asm_amd64.s
If I try to build it using go build, toolchain will use go tool asm to assemble the *.s files.
But if I add Cgo to the mixture, by putting a single import "C" into any of the sources, go will switch to gcc assembler.
I can see it by executing go build -n. Calls to the /usr/local/go/pkg/tool/linux_amd64/asm from the first case get replaced by calls to gcc. Besides that, it starts complaining about broken syntax.
Is this behaviour documented, so I can rely on it for the maintaining of my package? Can I force go build to use one exact assembler?
Yes, it's in the cgo documentation
When the Go tool sees that one or more Go files use the special import
"C", it will look for other non-Go files in the directory and compile
them as part of the Go package. Any .c, .s, or .S files will be
compiled with the C compiler. Any .cc, .cpp, or .cxx files will be
compiled with the C++ compiler. Any .h, .hh, .hpp, or .hxx files will
not be compiled separately, but, if these header files are changed,
the C and C++ files will be recompiled. The default C and C++
compilers may be changed by the CC and CXX environment variables,
respectively; those environment variables may include command line
options.

Error in CodeBlocks C++ program and how to set default main class

I have included the boost library in a Codeblocks c++ project.
Now, in the file
boost/function.hpp
there is an include statement
#include <boost/preprocessor/iterate.hpp>
However I get this error in Codeblocks when I try and compile:
/home/arvind/Documents/Workspace/Browser/boost/function.hpp|15|fatal error:
boost/preprocessor/iterate.hpp: No such file or directory|
What am I doing wrong here? I have simply included the Boost library as it is.
Also, I cannot find the screen/option to set the main class (which will actually execute).
How do I do this?(I am new to CodeBlocks hence this question).
Your boost includes seem to be in a non-standard/system directory : /home/arvind/Documents/Workspace/Browser, you must tell the compiler to look there (gcc -I command-line switch).
Go to Project->Build Options->Search Directories->Compiler and add the directory where boost includes are. I don't have a codeblocks install right here so this was from here.
If you can, I would recommand installing boost on your system once and for all instead of just copying files in your codeblocks workspace.

Linking a .s file with CMake

I have a c function that I'd like to use but thats compiled with the Intel compiler instead of the gnu C compiler. I'm using cmake to build the program.
(I'm actually using ROS and hence rosmake but the base is cmake so I think its more of a cmake issue than a ROS issue).
Suppose the file built with icc is x.c and produces an x.s file. I want to use the function a() from x.c in my file y.cpp.
In y.cpp I have:
#include "x.h"
.....
call a()
which works if CMakeLists.txt has
rosbuild_add_executable(y y.cpp x.c)
rosbuild_add_executable is analogous to add_executable(...)
but if I build x.c with icc and try to include the x.s file instead:
rosbuild_add_executable(y y.cpp x.s)
It doesnt work. Is there some change I should make to the way I call a() in y.cpp? or is there some other way to link it.
When using gcc, you can compile .S files with your C compiler (no explicit invocation of asm needed). CMake can be told to do so using
set_property(SOURCE <myfile>.S PROPERTY LANGUAGE C)
for each of your .S files.
To work with .s files you'll have to enable assembly language support in CMake with enable_language.
You can find more information here: https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/languages/Assembler

dev cpp win32 the program can't start because sqlite3.dll is missing

I am using dev c++ IDE which used ming gcc(i am not sure of it)
I wanted to use sqlite3 in my win32 c application
I downloaded sqlite3.dll and sqlite3.dev and used dlltool to create a .a file like libsqlite3.a and pasted in the lib folder of dev cpp and added this path in the project options -> parameters
i copied the header file sqlite3.h into the include folder(which i got from another website - http://www.opensource.apple.com/source/SQLite/SQLite-74/derived_source/sqlite3.h)
i executed the program and got the message 'the program can't start because sqlite3.dll is missing in your computer'
so i copied the sqlite3.dll into my working directory and then it worked
BUT
How to make the sqlite.* static while compiling?
I mean i thought by including the libsqlite3.a, the final exe will not be dependent of any external dll's.
So i want to know how to compile in a way that i will not be needing a dll and by doing so it makes my windows program standalone.
do i have a create a .lib file instead of .a file?
EDIT after answers and comments:
Besides, the devpak is working fine... yet i wanted to know how to include files to project or to create .a files so i am trying this way because if some components do not provide devpak then this will be the way we need to compile.. isn't it?
EDIT to show what i have done after the answer by CL and the two comments
This is how i have added the sqlite.c to project list
Here is the compile log
Compiler: Default compiler
Building Makefile:
"C:\Users\jayapalc\Documents\test-sqlite\Makefile.win"
Executing make...
make.exe -f "C:\Users\jayapalc\Documents\test-sqlite\Makefile.win" all
g++.exe -c sqlite3.c -o sqlite3.o -I"lib/gcc/mingw32/3.4.2/include"
-I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include"
sqlite3.c: In function `void strftimeFunc(sqlite3_context*, int,
sqlite3_value**)':
sqlite3.c:14727: error: invalid conversion from void*' tochar*'
The files i got in sqlite.org/sqlite-amalgamation-3071502.zip are
shell.c, sqlite3.h, sqlite3.c, sqlite3ext.h and i saw in other discussions that shell.c is not needed...
Besides, people were talking about gcc and g++... .
Apart from updating Dev-C++ itself, try this to compile sqlite.c as a C file:
Go to Project >> Project Options >> Files.
Find the C file we're talking about. Untick "Compile file as C++".
This should inform Dev-C++ that it should invoke gcc.exe, and not g++.exe.
If you don't want to compile sqlite yourself by adding it to your project, you can try passing the -static flag to GCC/G++ to force it to link libsqlite.a statically.
Just include the sqlite3.c file in your project.
You need only this filed, and it must be compiled as C, not C++.
Apparently, Dev-C++ does not allow mixing C and C++ source files in one project.
Instead, you could try to compile sqlite3.c as C and then include the generated .o file into the C++ project (on the Linker page).

Resources