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.
Related
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."
}
]
}
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
this is my code
#include<stdio.h>
int main()
{
printf("I just started");
return 0;
}
and this is the error
PS E:\coding> cd "e:\coding\" ; if ($?) { gcc tempCodeRunnerFile.c -o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile }
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
The main issue is that "YOU DIDN'T save YOUR FILE", first save your cpp file then enter the command (g++ file.cpp) or run it.
Usually, this error occurs when file was not saved.
so click anywhere on your file and press ctrl+s to save it again,
NOW YOUR ERROR GOT FIXXED, ......................
I'm new in stack overflow and I want to get some idea how I can build c binary(NOT executable program) on Gradle.
Now, I could not build c binary(NOT executable program). There is an error, however I don't know how to change build.gradle file.
terminal error is ...
> Executing task: gradle build <
:compileTestExecutableTestC
:linkTestExecutable
/opt/sparc/bin/.../sparc/lib/crt0.o: In function `zerobss':
/home/build/.../crt0.S:71: undefined reference to `main'
collect2: ld returned 1 exit status
:linkTestExecutable FAILED
output.txt is...
See file:///home/ethan/gradle_test/vscode_example/build/tmp/linkTestExecutable/output.txt for all output for linkTestExecutable.
linking test failed.
/opt/...lib/crt0.o: In function `zerobss':
/home/build/.../crt0.S:71: undefined reference to `main'
collect2: ld returned 1 exit status
Finished linkTestExecutable, see full log file:///home/ethan/gradle_test/vscode_example/build/tmp/linkTestExecutable/output.txt.
The c binary does not include 'main' function, so I got this error in my opinion. The c binary runs on power-up and provides bootrom entry code.
The build.gradle is below I used to build c binary.
apply plugin: 'c'
model {
components{
test(NativeExecutableSpec){ // test should be src/<folder> name. and <folder> should include cpp for cpp compilation
targetPlatform("sparc_test") // should be platforms item(sparc_test)
targetBuildTypes("release")
binaries.all {
cCompiler.args '-c -Wall'
linker.args '--cref -N --verbose'
}
sources {
c {
source {
srcDir "./src/test/c/rom_eeprom"
include "*.c"
}
exportedHeaders {
srcDirs "./src/test/c/include"
}
}
}
}
}
platforms{
sparc_test{ // should not use '-'. sparc_test can by any word
architecture "sparc" // sparc can be any word
}
}
toolChains{
sparc_gcc(Gcc) { // sparc_gcc can by any thing. Gcc should be used
target("sparc_test") // define platform tool chain
{
path '/opt/bcc/sparc/bin' // tool chain path
cCompiler.executable 'sparc-gcc' // C compiler
cppCompiler.executable 'sparc-g++' // C++ compiler
assembler.executable 'sparc-gcc'
linker.executable 'sparc-gcc'
}
}
}
buildTypes{
release
}
}
The c binary program code consists of rom.S and eeprom.c.
rom.S eeprom.c I do want the build.gradle to work like below
| | ==> compile
v v
rom.o eeprom.o
| |
--------------------
| ==> link
v
rom_eeprom.elf
| ==> objcopy
v
rom_eeprom.bin
How can I build this c binary program successfully?
Any suggestion is helpful, Thanks
First, Thanks to #thebusybee.
From #thebusybee answer, I changed the compiler and linker options, but the linking failed.
linking failed error occurred. Linker is finding main function.
Even though I added assembler plugin and assembly code, gradle can not compile assembly code(because assembler code's object file is not generated). And also gradle can not link object files as make did.
Here is my build.gradle
apply plugin: 'c'
apply plugin: 'assembler'
model {
components{
test(NativeExecutableSpec){ // test should be src/<folder> name. and <folder> should include cpp for cpp compilation
targetPlatform("sparc") // should be platforms item(sparc)
targetBuildTypes("release")
binaries.all {
cCompiler.args '-c -mv8 -Wall -fno-builtin -O2 -O'
linker.args '--cref -N --verbose -Map bl_low.map -T linkprom'
assembler.args '-xarch=v8'
}
sources {
c {
source {
srcDir "./src/test/c/bl_low"
include "*.c"
}
exportedHeaders {
srcDirs "./src/test/c/include"
}
}
}
sources {
asm {
source {
srcDir "./src/test/c/bl_low"
include "**/*.S"
}
}
}
}
}
platforms{
sparc{ // should not use '-'. sparc can by any word
architecture "sparc-v8" // sparc can be any word
}
}
toolChains{
sparc_gcc(Gcc) { // sparc_gcc can by any thing. Gcc should be used
target("sparc") // define sparc platform tool chain
{
path '/opt/bcc/sparc-elf-4.4.2/bin/' // tool chain path
cCompiler.executable 'sparc-elf-gcc' // C compiler
cppCompiler.executable 'sparc-elf-g++' // C++ compiler
assembler.executable 'sparc-elf-gcc' // work with sparc-elf-g++ rather than sparc-elf-as
linker.executable 'sparc-elf-gcc' // work with sparc-elf-g++ rather than sparc-elf-ld
}
}
}
buildTypes{
release
}
}
How can I change build.gradle to compile assembly code and link objects, then generate .elf file?
Finally, I decided not to use gradle on building for c, assembly project. Instead, I try to use bazel... Thank you #thebusybee and, sorry not to complete this problem.
Anyway, the last my build.gradle is...
apply plugin: 'c'
apply plugin: 'assembler'
model {
components{
test(NativeExecutableSpec){ // test should be src/<folder> name. and <folder> should include cpp for cpp compilation
targetPlatform("leon3_ft") // should be platforms item(leon3_ft)
targetBuildTypes("release")
binaries.all {
cCompiler.args "-mv8", "-Wall", "-fno-builtin"
linker.args "-Xlinker", "--cref", "-Xlinker", "-N", "-Xlinker", "--verbose", "-Xlinker", "-Map", "-Xlinker", "bl_low.map", "-Xlinker", "-T", "-Xlinker", "linkprom"
assembler.args "-mv8", "-Wall", "-fno-builtin"
}
sources {
c {
source {
srcDir "./src/test/c/bl_low"
include "*.c"
}
exportedHeaders {
srcDirs "./src/test/c/include", "./src/test/c/bl_low"
}
}
asm {
source {
srcDir "./src/test/asm"
include "*.s"
}
}
}
}
}
platforms{
leon3_ft{ // should not use '-'. leon3_ft can by any word
architecture "sparc-v8" // sparc can be any word
}
}
toolChains{
sparc_gcc(Gcc) { // sparc_gcc can by any thing. Gcc should be used
target("leon3_ft") // define leon3_ft platform tool chain
{
path '/opt/bcc/sparc-elf-4.4.2/bin/' // tool chain path
cCompiler.executable 'sparc-elf-gcc' // C compiler
cppCompiler.executable 'sparc-elf-g++' // C++ compiler
assembler.executable 'sparc-elf-gcc' // Assembler. Use GCC
linker.executable 'sparc-elf-ld' // work with sparc-elf-g++ rather than sparc-elf-ld
}
}
}
buildTypes{
release
}
}
Error message is...
> Executing task: gradle clean; gradle build <
BUILD SUCCESSFUL in 3s
1 actionable task: 1 executed
> Task :assembleTestExecutableTestAsm FAILED
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s: Assembler messages:
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:61: Error: Unknown opcode: `func_export(_romwindow_overflow)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:62: Error: Unknown opcode: `func_export(_romwindow_underflow)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:63: Error: Unknown opcode: `func_export(_romInit)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:64: Error: Unknown opcode: `func_export(romInit)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:65: Error: Unknown opcode: `data_export(_sdata)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:66: Error: Unknown opcode: `func_export(_cold)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:67: Error: Unknown opcode: `func_export(bl_low)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:74: Error: Unknown opcode: `func_import(romStart)'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:83: Error: Unknown opcode: `_wrs_text_seg_start'
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:108: Error: Unknown opcode: `bad_trap '
/home/ethan/gradle_test/vscode_example/src/test/asm/romInit.s:109: Error: Unknown opcode: `bad_trap '
...
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':assembleTestExecutableTestAsm'.
> A build operation failed.
Assembler failed while compiling romInit.s.
See the complete log at: file:///home/ethan/gradle_test/vscode_example/build/tmp/assembleTestExecutableTestAsm/output.txt
> Assembler failed while compiling romInit.s.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
1 actionable task: 1 executed
<The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
If you have a working makefile you can transfer all the options of the commands to the Gradle config.
You can call make with -n which prints the commands without executing them. The additional option -B pretends that all targets have to be build. So the command to see all commands to build your binary will be:
make -nB
I was sure that there is a -nostartfiles option on the linker command. ;-) But from your comment we now know that the command lines are:
sparc-elf-gcc -c -mv8 -Wall -fno-builtin -O2 -O ../include -c romInit.S
sparc-elf-gcc -c -mv8 -Wall -fno-builtin -O2 -O ../include -c eeprom.c
sparc-elf-gcc -c -mv8 -Wall -fno-builtin -O2 -O ../include -c bl_low.c
sparc-elf-ld --cref -N --verbose -Map bl_low.map -T linkprom -o bl_low.elf romInit.o memcpy.o memset.o strlen.o crc.o led.o eeprom.o bl_low.o
sparc-elf-objcopy -Obinary -v -S -g -x -X bl_low.elf bl_low.bin
And from these we find the compiler and linker flags:
Compiler: -c -mv8 -Wall -fno-builtin -O2 -O (the last -c is a duplicate)
Linker: --cref -N --verbose -Map bl_low.map -T linkprom
And there is a final build step to convert the ELF file into a binary file.
Because your linker script linkprom most probably does not include the standard startup code, there is no need for a main() function.
I installed graphics.h library on my Ubuntu 19.04, and when I write some code in my CLion IDE, it successfully includes the graphics.h Header file, and also after writing some basic functions, the IDE doesn't show any errors, but when I compile the Code it gives Errors.
My Code:
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, NULL);
line(10, 10, 50, 50);
closegraph();
}
My CMakeList.txt File:
cmake_minimum_required(VERSION 3.14)
project(CGR_MP C)
set(CMAKE_C_STANDARD 11)
include_directories("//usr//local//lib//libgraph-1.0.2//")
build_command("lgraph")
add_executable(CGR_MP main.c)
Errors:
====================[ Build | CGR_MP | Debug ]==================================
/snap/clion/73/bin/cmake/linux/bin/cmake --build /home/prathamesh/Desktop/College/Sem-2/CGR-MP/cmake-build-debug --target CGR_MP -- -j 2
Scanning dependencies of target CGR_MP
[ 50%] Building C object CMakeFiles/CGR_MP.dir/main.c.o
[100%] Linking C executable CGR_MP
/usr/bin/ld: CMakeFiles/CGR_MP.dir/main.c.o: in function `main':
/home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:7: undefined reference to `detectgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:8: undefined reference to `initgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:10: undefined reference to `line'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:12: undefined reference to `closegraph'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CGR_MP.dir/build.make:84: CGR_MP] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/CGR_MP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/CGR_MP.dir/rule] Error 2
make: *** [Makefile:118: CGR_MP] Error 2
Despite everything being successfully installed, it shows all Graphics functions as undefined reference