How do I compile with external header file in sublime text in C? - c

I use one header named header.h in main.c.
The function test is announced in header.h and defined in test.c.
However, it says words below even though I use build system as C.
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"test(int)", referenced from:
_main in main-a4d82e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.6s with exit code 1]
[cmd: ['bash', '-c', 'g++ -Wall -std=c++11 -O2 \'/Users/hanlock/Documents/CODE/TEST/TEST/main.c\' -o \'/Users/hanlock/Documents/CODE/TEST/TEST/main\' && osascript -e \'tell application "Terminal" to activate do script " cd \\"/Users/hanlock/Documents/CODE/TEST/TEST\\" &&start_ms=$(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\")&&clear&&\\"/Users/hanlock/Documents/CODE/TEST/TEST/main\\" &&elapsed_ms=$(($(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\") - start_ms))&& read -p \\"Press Enter to exit($elapsed_ms ms).\\"&&exit"\'']]
[dir: /Users/hanlock/Documents/CODE/TEST/TEST]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
I've read similar questions about this in C++. However, it seems not working in my case.
So, here comes the problem. How can I use external header file with sublime in language C?
Here is the code:
main.c
#include <stdio.h>
#include "./Header.h"
int main(int argc, const char * argv[]) {
test(1);
return 0;
}
header.h
#ifndef Header_h
#define Header_h
int test(int);
#endif
test.c
#include <stdio.h>
int test(int i){
printf("%d\n",i);
return 0;
}

In test.c:
#include "header.h"
In your shell:
gcc -I [dir] test.c
This will include any external header named header.h located in dir.
I hope this answers your question.

Related

how to solve this problem compiler error that linker command failed in VSCODE

I wrote some C code on VSCODE like that.
The code is divided into three files: the header, the function, and main in same project folder.
But when I started compile, files are can't compile and error. like terminal text.
Maybe I think this error is linking error..
How to solve this problem..?
[source code]
mysqrt.c
#include "mysqrt.h"
#include <stdio.h>
#include <math.h>
double mysqrt(double a, double b){
double result = sqrt(pow(a,2)+pow(b,2));
return result;
}
mysqrt.h
#include <stdio.h>
double mysqrt(double a, double b);
mysqrtTest.c
#include <stdio.h>
#include <math.h>
#include "mysqrt.h"
void main(void){
double sum = mysqrt(3,4);
printf("%.2f\n",sum);
}
[terminal text]
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: warning:
return type of 'main' is not 'int' [-Wmain-return-type]
void main(void){
^
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: note: change
return type to 'int'
void main(void){
^~~~
int
1 warning generated.
Undefined symbols for architecture x86_64:
"_mysqrt", referenced from:
_main in mysqrtTest-45c3c1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[OS]: macOS Mojave 10.14.6
to resolve your linking issue you need to compile all your c file, as dependencies are not automatically resolved ( header files could be name separately from code file so .h files and .c files are independant ).
# assuming that gcc is your compiler
gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c -lm
Although I would recommend you to learn about separate compilation, and using a build system, like make
Example of Makefile
mysqrtTest: mysqrtTest.o mysqrt.o
${CC} -o $# $^ -lm
then use make to build your binary

Creating C DLL and using it in Golang?

I want to create DLL in C and use it in Golang.
I used this tutorial to generate dll :
helloWorld.h
#include<stdio.h>
void __stdcall __declspec(dllexport) hello();
helloWorld.c
#include<stdio.h>
#include "helloWorld.h"
__stdcall void hello()
{
printf("Hello World !!");
}
I used this in command Prompt to compile it
g++ -c helloWorld.c
g++ -shared -o helloWorld.dll helloWorld.o -Wl,--out-implib,libhelloWorld.a
I was able to use the generated dll in this C code , named as example.c :
#include<stdio.h>
#include "helloWorld.h"
int main()
{
hello();
}
and compile it using
g++ -c example.c
g++ -o example.exe example.o -L. -lhelloWorld
But while using the DLL in Golang , i am getting error , please help me
test.go
package main
/*
#cgo LDFLAGS: -L. -lhelloWorld
#include "helloWorld.h"
*/
import "C"
func main(){
C.hello()
}
Error :
# command-line-arguments
C:\Users\kumarmoh\AppData\Local\Temp\go-build544493490\b001\_x002.o: In function `_cgo_525f579e070a_Cfunc_hello':
/tmp/go-build/cgo-gcc-prolog:40: undefined reference to `hello'
collect2.exe: error: ld returned 1 exit status
error image
Other info :
go version go1.11 windows/amd64
64-bit OS , x64-based processor , Windows 10
I tried creating dll in Visual studios and it gave me error as explained here .

Error in compiling before linking the object files using gcc

I have source files written in C programming using notepad++ and I am running them from command lines and later i need to link them inorder to generate the .exe file.
Here are the following commands I want to use while generating .exe file
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
But when i run the following command my compiler is returning with the following error status.
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain#16'
when i run the following command to compile my mainc file
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
And here are my mainc.c and logc.c and logc.h files for your reference
logc.c file is here
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c file is here
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
and logc.h file is here
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
How can i compile individual source files and then link them and generate an executable file.
Thanks in advance.
You don't create object files, for that you need the -c argument:
gcc logc.c -c
gcc mainc.c -c
gcc -o output logc.o mainc.o
By default gcc will generate an executable file, not an object file. So when you compile logc.c, it tries to make an executable but it can't find the main function so it fails. Similarly with main.c, it tries to make an executable but can't find Log
You need to add the -c option to create object files:
gcc logc.c -c -o logc.o
gcc mainc.c -c -o mainc.o

Link tidylib library to C application

I am trying to use the tidylib library within my C application. When compiling, I get the following errors:
$ make
rm -f sbo-export
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Undefined symbols for architecture x86_64:
"_tidyCreate", referenced from:
_safari_init_session in safarilib-c7ab6a.o
"_tidyParseString", referenced from:
_safari_init_session in safarilib-c7ab6a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [default] Error 1
safarilib.c:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tidy/tidy.h>
#include <tidy/buffio.h>
#include <curl/curl.h>
#include "safarilib.h"
void usage( char *username, char *password )
{
TidyDoc tdoc = tidyCreate();
char *input = "<html><body><h1>Hello World!</h1></body></html>";
tidyParseString( tdoc, input );
}
Any suggestions?
Further Infos:
For installing libtidy, I did the following:
Downloaded libtidy from http://tidy.sourceforge.net and then
followed the instructions form the file tidy/build/readme.txt
My library is installed here:
/usr/local/Cellar/libtidy
/usr/local/Cellar/libtidy/bin
/usr/local/Cellar/libtidy/bin/tab2space
/usr/local/Cellar/libtidy/bin/tidy
/usr/local/Cellar/libtidy/include
/usr/local/Cellar/libtidy/include/buffio.h
/usr/local/Cellar/libtidy/include/platform.h
/usr/local/Cellar/libtidy/include/tidy.h
/usr/local/Cellar/libtidy/include/tidyenum.h
/usr/local/Cellar/libtidy/lib
/usr/local/Cellar/libtidy/lib/libtidy-0.99.0.dylib
/usr/local/Cellar/libtidy/lib/libtidy.a
/usr/local/Cellar/libtidy/lib/libtidy.dylib
/usr/local/Cellar/libtidy/lib/libtidy.la
From compiler option it's look you have not specified tidylib.
cc sbo-export.c safarilib.c -L/usr/local/lib -lcurl -L/usr/local/Cellar/libtidy/lib -I/usr/local/Cellar/libtidy/include -o sbo-export
Here you need to add -ltidy and probably path by -L .
Some additional info about linking.

Include a source file in a C program

How can I include foo() function of foo.c in this small program (sorry for my noob question):
In my foo.h file:
/* foo.h */
#include <stdio.h>
#include <stdlib.h>
int foo(double largeur);
In foo.c:
/* foo.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(double largeur)
{
printf("foo");
return 0;
}
And in main.c:
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int main(int argc, char *argv[])
{
printf("Avant...");
foo(2);
printf("Apres...");
return 0;
}
After compiling:
$ gcc -Wall -o main main.c
I get this error:
Undefined symbols: "_foo",
referenced from:
_main in ccerSyBF.o ld: symbol(s) not found collect2: ld
returned 1 exit status
Thanks for any help.
$ gcc -Wall -o main main.c foo.c
GCC doesn't know to look for foo.c if you don't tell it to :)
Creating a program in C requires two steps, compiling and linking. To just run the compiling part, use the -c option to gcc:
gcc -c main.c
This creates an object file, main.o (or main.obj on Windows). Similarly for gcc -c foo.c. You won't get the error message above at this stage. Then you link these two object files together. At this stage, the symbol foo is resolved. The reason you got the error message was because the linker couldn't find the symbol, because it was only looking at main.o and not foo.o. The linker is usually run from gcc, so to link your object files and create the final executable file main, use
gcc -o main main.o foo.o
You have to compile foo.c also because it is another module. Let me see how they do it in gcc:
$ gcc -Wall main.c foo.c -o main
You could also do this in your MakeFiles, like this:
APP_NAME = Foo
Foo_HEADERS = foo.h
Foo_FILES = main.c foo.c
If you're not so much familiar with MakeFiles i suggest you to take a look at Make Docs, but this is a simple example, APP_NAME sets the name of the compiled executable(in this case is Foo), Foo_HEADERS will set the headers used by your application, Foo_FILES you will set the source files of your applications, remember to put the APP_NAME(in this case Foo) at the beginning of _HEADERS and _FILES. I suggest you to use MakeFiles because they will organize you application build process and will be better for the end-user.

Resources