Asterisk (*) is not working in VSCodes tasks.json - c

When I try to run a C project with multiple modules it by default only compiles the selected module. I tried editing the tasks.json to compile every C and header file in a folder but it just passes the asterisk as an asterisk to my command line.
I edited the tasks.json and tried to build the project with the following output:
Starting build...
/usr/bin/gcc -f diagnostics-color=always -g /home/user/VSCodeProjects/multimodule/main.c /home/user/VSCodeProjects/multimodule/*.c /home/user/VSCodeProjects/multimodule/*.h -o /home/user/VSCodeProjects/multimodule/main
/usr/bin/ld: /tmp/ccgdRy3t.o: in function `main':
/home/user/VSCodeProjects/multimodule/main.c:4: multiple definition of `main'; /tmp/ccW8LBPU.o:/home/user/VSCodeProjects/multimodule/main.c:4: first defined here
collect2: Fehler: ld gab 1 als Ende-Status zurück
Build finished with error(s).
As seen here it just uses the asterisk when running the gcc command instead of addressing all .c and .h files in the folder.
I already searched for the issue and found some issues e.g. this issue on the VSCode GitHub page, but none of the solutions seemed to work. Most of the answers I found don't apply here, as I'm already using version 2.0.0.
Contents of main.c:
#include "test.h"
int main(void)
{
printTest();
return 0;
}
Contents of test.c:
#include "test.h"
#include<stdio.h>
void printTest(){
printf("Test");
}
Contents of test.h:
#if !defined(TEST_H)
#define TEST_H
void printTest();
#endif // TEST_H
Contents of tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}/*.c",
"${fileDirname}/*.h",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}

Related

undefined reference to `DAQmxCreateTask#8

By using VS code, I was trying to build a C code which has a DAQmx library from National Instruments. I have already included the NIDAQmx.h header file to the code. even if the build was successful, while compiling the code I am getting error messages regarding undefined reference to DAQmx functions. 
find my code below,
#include <stdio.h>
#include "C:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\include\NIDAQmx.h"
TaskHandle taskHandle=0;
int ret=0;
int main()
{
printf("Hello world");
ret=DAQmxCreateTask("task",&taskHandle);
printf("Return for creating task is %d\n",ret);
DAQmxStopTask (taskHandle);
DAQmxClearTask(taskHandle);
printf("Task closed ");
}
my console output as follows,
[Running] cd "d:\Documents\mydaq\src\" && gcc mydaq.c -o mydaq && "d:\Documents\mydaq\src\"mydaq
C:\Users\Remya\AppData\Local\Temp\cc88k2NN.o:mydaq.c:(.text+0x32): undefined reference to `DAQmxCreateTask#8'
C:\Users\Remya\AppData\Local\Temp\cc88k2NN.o:mydaq.c:(.text+0x5c): undefined reference to `DAQmxStopTask#4'
C:\Users\Remya\AppData\Local\Temp\cc88k2NN.o:mydaq.c:(.text+0x6c): undefined reference to `DAQmxClearTask#4'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.421 seconds
my tasks. Json file as follows.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"mydaq.c",
"-o",
"mydaq.exe",
"-LC:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib32\\msvc",
"-lNIDAQmx"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Build finished successful in VS Code terminal as follows,
Starting build...
C:\MinGW\bin\gcc.exe -g mydaq.c -o mydaq.exe "-LC:\Program Files (x86)\National Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc" -lNIDAQmx
Build finished successfully.

error about compiling multi files on vscode

I'm a beginner and I tried to run a simple project but received
o:main.c:(.text+0xde): undefined reference to `max'collect2.exe: error: ld returned 1 exit status.
I have searched on the Internet for hours but still failed. I really need your help. Thanks
my project :
my first try
I searched on the Internet and realized visual studio code compile one file one time. So I modify my tasks.json as follows
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:\\TDM-GCC-64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
//I modify the following sentences as
"${fileDirname}\\function.c",
"${fileDirname}\\main.c",
"${fileDirname}\\main.h",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
But I still received
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\DELL\AppData\Local\Temp\ccZp5xjY.o:main.c:(.text+0xde): undefined reference to `max'
collect2.exe: error: ld returned 1 exit status
by the way, my project :
enter image description here
and my settings
enter image description here
2.my second try
I use the MakeFile but still the same error(another project but also a simple one)
MakeFile
sum : main.o sum.o
gcc main.o sum.o -o sum
main.o : main.o sum.h
gcc -c main.c
sum.o : sum.c sum.h
gcc -c sum.c
main.c
#include <stdio.h>
#include "sum.h"
int main()
{
printf("%d\n", sum(8,3));
// printf("1");
return 0;
}
sum.c
#include "sum.h"
int sum(int a, int b)
{
return a + b;
}
sum.h
int sum(int a, int b);
error
enter image description here
so I doubt if I should adjust some settings of vscode
Undefined reference errors happen when the compiler can't find the function that you are referencing. This is usually due to missing/incorrect arguments in one's Makefile.
I see you have a header file in the arguments of tasks.json, which is unnecessary.
EDIT: You did not put '-o' as an argument for main.o and sum.o You also put main.o as a required file for main.o, which wont work.
FIX:
sum: main.o sum.o
gcc -o sum main.o sum.o
main.o: main.c
gcc -o main.o -c main.c
sum.o: sum.c
gcc -o sum.o -c sum.c

unable to get preprocessed source file or object file if I include header from a different directory

my folder/file structure:
testing
folder
head.h
main.c
main.c
#include "head.h"
int main(){
foo(3);
return 1;
}
head.h
void foo(int x){
x = 5;
}
in cmd I go to "testing" folder and type in:
gcc -c -lfolder main.c -o main.o
but that gets me an error:
main.c:1:18: fatal error: head.h: No such file or directory
#include "head.h"
From what I understand, flag -ldir_name specifies that compiler should look inside of that folder for any #include files.
Can you please help me undersand what I am doing wrong. I also tried getting preporcessor's output:
gcc -E -lfolder main.c -o main.i
again this should look for head.h in "folder", paste its content inside of main.c

How do I compile with external header file in sublime text in 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.

C plugin system: dlopen fails

as a continuation to this post C pluginsystem: symbol lookup error, I am still writing my plugin system and encounter new bugs.
To recap what the plugins are, the program consists of a network application interfaced by a shell, messages has a type and therefore can be use to create applications on the network. For exemple, a possible application would be a chat or a transfert application.
So shell commands can send message of a particular application on the network, when a message is received, if it corresponds to a particular application then an action function is executed with the message content as argument, it could be the application.
A plugin is a shared library with an init function that register it's commands and actions. A command could just be a simple command that doesn't interact with the network, and that's why I achieved at the moment.
The plugin system consists in modules:
plugin_system.c
list.c used by the first module to store plugins
The network part consists in:
protocol.c main part of the protocol
message.c main part for message treatment
application.c main part used to program applications
common.c file with ccommon functions
network.c useful network functions
The modules in protocole are all interdependent, I have split files for conveniency.
All modules are compiled with -fPIC option.
To compile a plugin called plug.c wich doesn't interact with the network, I use:
gcc -Wall -O2 -std=gnu99 -D DEBUG -g -fPIC -c -o plug.o plug.c
gcc -Wall -O2 -std=gnu99 -D DEBUG -g -o plug.so plug.o plugin_system.o list.o -shared
And it works perfectly, the library is loaded with dlopen with no problem, the init function loaded with dlsym and executed correctly so the plugin is registered, I then executed the command and I can see that it work.
Now I wan't to add supports for network communications for the plugins, so I have modified the same test plugin that I used which has just a command to print a message. I have had a call to sendappmessage_all a function that send a message to everyone over the network, defined in message.c.
I can compile the new plugin without adding the network module objects, it compile, the plugin loads correctly, but when it call sendappmessage_all obviously it fails with the message
symbol lookup error: ./plugins/zyva.so: undefined symbol: sendappmessage_all
So to make it work, I should like the plugin with network modules so that's what I have done with
gcc -Wall -O2 -std=gnu99 -D DEBUG -g -o plug.so plug.o plugin_system.o list.o protocol.o message.o thread.o common.o application.o network.o -shared
It compile but when I try to load the plugin, dlopen return NULL.
I have also tried to add just one module, at worst it would only result in an undefined symbol error, but I dlopen still return NULL.
I know it's a lot of informations and on the otherside you probably wan't to see the code but I tried to be the clearer in the most succint way I could be because is way more complex and bigger than the post.
Thank you for your understanding.
The problem is that when you compile the plugin system (i.e. functions called by plugins), and link it to the final executable, the linker does not export the symbols used by the plugins in the dynamic symbol table.
There are two options:
Use -rdynamic when linking the final executable, adding all symbols to the dynamic symbol table.
Use -Wl,-dynamic-list,plugin-system.list when linking the final executable, adding symbols listed in file plugin-system.list to the dynamic symbol table.
The file format is simple:
{
sendappmessage_all;
plugin_*;
};
In other words, you can list either each symbol name (function or data structure), or a glob pattern that matches the desired symbol names. Remember the semicolon after each symbol, and after the closing brace, or you'll get a "syntax error in dynamic list" error at link time.
Note that just marking a function "used" via __attribute__((used)) is not sufficient to make the linker include it in the dynamic symbol table (with GCC 4.8.4 and GNU ld 2.24, at least).
Since the OP thinks what I wrote above is incorrect, here is a fully verifiable proof of the above.
First, a simple main.c that loads plugin files named on the command line, and executes their const char *register_plugin(void); function. Because the function name is shared across all plugins, we need to link them locally (RTLD_LOCAL).
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <stdio.h>
static const char *load_plugin(const char *pathname)
{
const char *errmsg;
void *handle; /* We deliberately leak the handle */
const char * (*initfunc)(void);
if (!pathname || !*pathname)
return "No path specified";
dlerror();
handle = dlopen(pathname, RTLD_NOW | RTLD_LOCAL);
errmsg = dlerror();
if (errmsg)
return errmsg;
initfunc = dlsym(handle, "register_plugin");
errmsg = dlerror();
if (errmsg)
return errmsg;
return initfunc();
}
int main(int argc, char *argv[])
{
const char *errmsg;
int arg;
if (argc < 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
fprintf(stderr, " %s plugin [ plugin ... ]\n", argv[0]);
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}
for (arg = 1; arg < argc; arg++) {
errmsg = load_plugin(argv[arg]);
if (errmsg) {
fflush(stdout);
fprintf(stderr, "%s: %s.\n", argv[arg], errmsg);
return EXIT_FAILURE;
}
}
fflush(stdout);
fprintf(stderr, "All plugins loaded successfully.\n");
return EXIT_SUCCESS;
}
The plugins will have access via certain functions (and/or variables), declared in plugin_system.h:
#ifndef PLUGIN_SYSTEM_H
#define PLUGIN_SYSTEM_H
extern void plugin_message(const char *);
#endif /* PLUGIN_SYSTEM_H */
They are implemented in plugin_system.c:
#include <stdio.h>
void plugin_message(const char *msg)
{
fputs(msg, stderr);
}
and listed as dynamic symbols in plugin_system.list:
{
plugin_message;
};
We'll also need a plugin, plugin_foo.c:
#include <stdlib.h>
#include "plugin_system.h"
const char *register_plugin(void) __attribute__((used));
const char *register_plugin(void)
{
plugin_message("Plugin 'foo' is here.\n");
return NULL;
}
and just to remove any confusion about what effect there is having each plugin a registration function by the same name, another plugin named plugin_bar.c:
#include <stdlib.h>
#include "plugin_system.h"
const char *register_plugin(void) __attribute__((used));
const char *register_plugin(void)
{
plugin_message("Plugin 'bar' is here.\n");
return NULL;
}
To make all of this easy to compile, we'll need a Makefile:
CC := gcc
CFLAGS := -Wall -Wextra -O2
LDFLAGS := -ldl -Wl,-dynamic-list,plugin_system.list
PLUGIN_CFLAGS := $(CFLAGS)
PLUGIN_LDFLAGS := -fPIC
PLUGINS := plugin_foo.so plugin_bar.so
PROGS := example
.phony: all clean progs plugins
all: clean progs plugins
clean:
rm -f *.o $(PLUGINS) $(PROGS)
%.so: %.c
$(CC) $(PLUGIN_CFLAGS) $^ $(PLUGIN_LDFLAGS) -shared -Wl,-soname,$# -o $#
%.o: %.c
$(CC) $(CFLAGS) -c $^
plugins: $(PLUGINS)
progs: $(PROGS)
example: main.o plugin_system.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $#
Note that Makefiles require intendation by tabs, not spaces; listing the file here always converts them to spaces. So, if you paste the above to a file, you'll need to fix the indentation, via e.g.
sed -e 's|^ *|\t|' -i Makefile
It is safe to run that more than once; the worst it can do, is mess up your "human-readable" layout.
Compile the above using e.g.
make
and run it via e.g.
./example ./plugin_bar.so ./plugin_foo.so
which shall output
Plugin 'bar' is here.
Plugin 'foo' is here.
All plugins loaded successfully.
to standard error.
Personally, I prefer to register my plugins via a structure, with a version number, and at least one function pointer (to the initialization function). This lets me load all plugins before initializing them, and resolve e.g. interplugin conflicts or dependencies. (In other words, I use a structure with a fixed name, rather than a function with a fixed name, to identify plugins.)
Now, as to __attribute__((used)). If you modify plugin_system.c into
#include <stdio.h>
void plugin_message(const char *msg) __attribute__((used));
void plugin_message(const char *msg)
{
fputs(msg, stderr);
}
and modify the Makefile to have LDFLAGS := -ldl only, the example program and plugins will compile just fine, but running it will yield
./plugin_bar.so: ./plugin_bar.so: undefined symbol: plugin_message.
In other words, if the API exported to plugins is compiled in a separate compilation unit, you will need to use either -rdynamic or -Wl,-dynamic-list,plugin-system.list to ensure the functions are included in the dynamic symbol table in the final executable; the used attribute does not suffice.
If you want all and only non-static functions and symbols in plugin_system.o included in dynamic symbol table in the final binary, you can e.g. modify the end of the Makefile into
example: main.o plugin_system.o
#rm -f plugin_system.list
./list_globals.sh plugin_system.o > plugin_system.list
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $#
with list_globals.sh:
#!/bin/sh
[ $# -ge 1 ] || exit 0
export LANG=C LC_ALL=C
IFS=:
IFS="$(printf '\t ')"
printf '{\n'
readelf -s "$#" | while read Num Value Size Type Bind Vis Ndx Name Dummy ; do
[ -n "$Name" ] || continue
if [ "$Bind:$Type" = "GLOBAL:FUNC" ]; then
printf ' %s;\n' "$Name"
elif [ "$Bind:$Type:$Ndx" = "GLOBAL:OBJECT:COM" ]; then
printf ' %s;\n' "$Name"
fi
done
printf '};\n'
Remember to make the script executable, chmod u+x list_globals.sh.

Resources