4

I'm trying to run the example from tiny cc (tcc-0.9.26-win64-bin.zip) called libtcc_test.c.

I've copied libtcc.h from libtcc into include and libtcc.def into lib.
Then I ran tcc ./examples/libtcc_test.c and got a linking error :/

tcc: error: undefined symbol 'tcc_new'
tcc: error: undefined symbol 'tcc_set_lib_path'
tcc: error: undefined symbol 'tcc_set_output_type'
tcc: error: undefined symbol 'tcc_compile_string'
tcc: error: undefined symbol 'tcc_add_symbol'
tcc: error: undefined symbol 'tcc_relocate'
tcc: error: undefined symbol 'tcc_get_symbol'
tcc: error: undefined symbol 'tcc_delete'

What am I missing ?


More info:

P:\cpp\tcc>tcc ./examples/libtcc_test.c -vv
tcc version 0.9.26 (i386 Win32)
-> ./examples/libtcc_test.c
-> p:/cpp/tcc/include/stdlib.h
->  p:/cpp/tcc/include/_mingw.h
->   p:/cpp/tcc/include/stddef.h
->   p:/cpp/tcc/include/stdarg.h
->  p:/cpp/tcc/include/limits.h
->  p:/cpp/tcc/include/sec_api/stdlib_s.h
->   p:/cpp/tcc/include/stdlib.h
->  p:/cpp/tcc/include/malloc.h
-> p:/cpp/tcc/include/stdio.h
->  p:/cpp/tcc/include/vadefs.h
->  p:/cpp/tcc/include/sec_api/stdio_s.h
->   p:/cpp/tcc/include/stdio.h
-> p:/cpp/tcc/include/string.h
->  p:/cpp/tcc/include/sec_api/string_s.h
->   p:/cpp/tcc/include/string.h
-> p:/cpp/tcc/include/libtcc.h
-> p:/cpp/tcc/lib/libtcc1.a
-> p:/cpp/tcc/lib/msvcrt.def
-> p:/cpp/tcc/lib/kernel32.def
tcc: error: undefined symbol 'tcc_new'
tcc: error: undefined symbol 'tcc_set_lib_path'
tcc: error: undefined symbol 'tcc_set_output_type'
tcc: error: undefined symbol 'tcc_compile_string'
tcc: error: undefined symbol 'tcc_add_symbol'
tcc: error: undefined symbol 'tcc_relocate'
tcc: error: undefined symbol 'tcc_get_symbol'
tcc: error: undefined symbol 'tcc_delete'
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32
  • Well you need to link against the libs and include files, to compile your code. Make sure you link and include with necessary libs and headers. – danglingpointer Apr 01 '17 at 10:35
  • I'm very new to c but there is #include "libtcc.h" and tcc ... -vv also output says that it is added but I don't know how to "load" .def file – Maciej Kozieja Apr 01 '17 at 10:40
  • @LethalProgrammer: "*... need to link against the ... include files ...*" included (header) files aren't used during the linking phase, but only during compilation, which is done when it comes to linking. – alk Apr 01 '17 at 10:43

2 Answers2

5

To link in a library, you need to add a -l${library_basename} flag after all c files or o files. If the library is named libtcc.a or libtcc.so (on Windows it's probably tcc.dll or libtcc.dll), you need to add -ltcc.

tcc  ./examples/libtcc_test.c  -ltcc

You might also need to add an -L flag to add a search path in case the library you want to link in is not your system's standard library directories:

tcc -L . ./examples/libtcc_test.c -ltcc
#also look for libtcc.so or libtcc.a in the current directory (.)

The libtcc_test.c from test/libtcc_test.c in the tinycc repo also needed the dl library (standard library for dynamic loading) to build:

tcc -L .  tests/libtcc_test.c  -ltcc -ldl #worked 

(it complained about undefined dlopen, dlclose, and dlsym which are known to come from libdl).

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
0

The following command worked on Windows:

cd your-tcc-directory
tcc -Ilibtcc -L. -ltcc examples/libtcc_test.c

You might want to add -run to skip generating exe file and running the source code directly.

I tried it on Linux, but it couldn't find libtcc.h. I guess the following will work (note -ltcc1 instead of -ltcc):

tcc -I/path/to/libtcc.h/location -L/usr/lib/tcc/x86-64 -ltcc1 path/to/libtcc_test.c
André Willik Valenti
  • 1,702
  • 14
  • 21