This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Part Number: MSP432P401R
Tool/software: Code Composer Studio
I am at a loss with a small project that I am working on. My Project builds fine but when I try to include a function that is declared in a .h file and defined in a .c file I get build errors. The errors say "unresolved symbol addition(int), first referenced in ./main.obj MixingCadnCPP_Code C/C++ Problem." This .c file and .h file are files I created with only one function inside it. See below for what I have.
.h file
#ifndef ADDITION_H_ #define ADDITION_H_ extern int addition(int); #endif /* ADDITION_H_ */
.c file
#include "addition.h" int add1(int x) { return x + 1; }
main.cpp
#include "addition.h" //LedManager manager; int main(void) { // Turn LED 1 on GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN2); int x = 0; x = addition(x); return 0; }
I do not understand why I can not create a c source file define the functions in its header file and simply call the functions in main when I need them. I feel like I am missing something small. Does anyone have any suggestions. Also I would like to keep my main as a c++ source file and my other c source file as C. I am trying to learn to call a c function from a .cpp file.
Thank you
For every function called from main, the compiler needs to find a definition. If the definition is in a source file, that file should be added to the project so it is compiled and linked along with the other files.
In your code, main is calling addition, but the function defined in the other .c file is add1, not addition. Perhaps it was a typo or oversight.
Xavier Cardenas said:Also I would like to keep my main as a c++ source file and my other c source file as C. I am trying to learn to call a c function from a .cpp file.
You can combine C and C++ code in the same program. Take a look at this related thread that points to helpful FAQ information.
Okay that makes sense but when you say, " that file should be added to the project so it is compiled and linked along with the other files." That's were I get lost. I thought by adding a header file into code composer studio build path that it would compile the c source file.
How do you actually get the c source filed compiled and linked? Do I need to make another makefile? I've tried looking in the build setting but I get lost with so many options.
Thank you
Xavier Cardenas said:I thought by adding a header file into code composer studio build path that it would compile the c source file.
No simply adding path to the header file is not sufficient. For the .c source file to be compiled and linked, it needs to be part of the project, and that can be done by either adding or linking the file to the project. You do not need to create any makefiles as the build process is managed by CCS.
For more information please review the CCS Users Guide. The specific section on adding/linking source file is here, but the other chapters should also be helpful in understanding the environment.
http://software-dl.ti.com/ccs/esd/documents/users_guide/ccs_project-management.html#adding-or-linking-source-files-to-project
Hope this helps.
Thank you, both the links that you provided for me answered my questions. Also you were right that the name of my function in the header file and in the main.cpp were mismatched.